1
votes

A 2D circle shaped player has the following code

Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = target;

which sets the player's position to the mouse position, and when it collides with enemies they get destroyed (simple concept).

The problem is that the enemies do not recognize collision when player passes through them quickly so they are not destroyed. This problem frustrated me I don't know why collision doesn't work when an object passes through another object quickly .

Is there a solution? Or it is just UnityEngine Maximum Performance

Thanks in advance.

1
If you want proper collision detection, you should try using a rigidbody (and physics) instead of directly modifying the player's position. Otherwise, you'll need to manually detect collisions yourself for consistent behaviour, either with raycasting or checking for colliders within a given space.Serlite
@Serlite thanks for your suggestion But I am already using rigidbody 2D with the values for AngularDrag and GravityScale are set to zero.Does it has to go with Collision Detection{Discrete,Continous}?Bak Stak

1 Answers

1
votes

I recommend you to see this link. It explains much about Collision in unity.It looks similar to your game also.

  1. You could also calculate distance between two objects and when the distance is equal to zero you may destroy that object too.

Note1: this destroys the object only when it is coinciding with another object.

  1. Or by calculating the outer distance between two objects when it is equal to zero you destroy that object.

Note2: this destroys the object when two objects are starting to collide.