- Home /
Player's collider doesn't touch obstacle's collider every time that it collides!
I've been trying to expand Unity's 2D Platformer Character Controller Tutorial, since I wanted to find a way of dealing with collisions between game objects without having to rely on OnCollision2D functions because I don't want game objects interacting between each other using physics. Therefore, I made a script where I give a value to a kinematic Rigidbody2D's velocity on the FixedUpdate function, then I check if a collision has happened. So I ended up with this function for the collision detection part:
public void CheckCollisionPlayer() {
distance = (rb.velocity * Time.deltaTime).magnitude;
// Checks if collision exists only if collision distance if bigger than the defined minimum
if (distance > minMoveDistance) {
count = rb.Cast (rb.velocity, contactFilter, hitBuffer, distance);
hitBufferList.Clear ();
for (int i = 0; i < count; i++) {
hitBufferList.Add (hitBuffer [i]);
}
// For all of the results of the Cast...
for(int i = 0; i < hitBufferList.Count; i++) {
// Transform position will stay the same as if it has not moved with the velocity.
transform.position = (Vector2)transform.position - (rb.velocity * Time.deltaTime);
}
}
}
I wanted to create a movement like of a space ship in 2D, where the player is going from the left to the right and could move vertically, being also able to collide with objects by using this function. Unfortunately, after testing it I found out that this is making my player Game Object stop before it collides in fact, like as if there is a gap between he and the other object, and I'm confused because when I change the player direction, as he is "colliding", it briefly touches the other collider before it begins to go in that direction.
Can someone please help me? I'm lost!
Try changing the collision type to continues ins$$anonymous$$d of discrete.
I just tried and... Sadly, I got the same result, but thanks for the idea hahah
Answer by JustinEllis · Jul 11, 2017 at 03:45 PM
I have the exact same problem. It has to do with moving objects using the transform of the object. Try using physics based movement and you should get better results. That or you need to stop trying to move the transform while its still touching the object. Its kind of a havk and it only makes a slight difference
Yeah, I guess my previous way of dealing with movement wasn't that good to begin with... But fortunately all I wanted to do was to specify exactly with what Game Objects my collider would check for collision, and I just found a way to do that! Using the Collision Layer $$anonymous$$atrix I can select the Layers (in my case, "Enemy" and "EnemyBullet") that my Game Object Layer ("Player") can collide, and still be able to use the OnCollision2D functions, making my movement much more precise (at least it looks so) and, I hope, more efficient!
But as I was curious, I tried to make the rigidbody's velocity equals to zero ins$$anonymous$$d of changing the transform value, and it looks like it works! I just had to do some $$anonymous$$or improvements on the section of FixedUpdate where I give a value to velocity to test before if the object was still colliding and trying to go in the same direction as before. I will stay with the OnCollision approach since it seems more intuitive and simple, but thanks you so much for your answers! It helped me a lot in problems that may occur in future projects.
Answer by springwater · Jul 11, 2017 at 05:51 AM
Try scaling down your colliders, they have controls for that in the inspector. Or scale up the graphics independently.
I tried it out but didn't work either... I guess I have a problem with my way of "stopping" the Game Object, because I'm just changing the transform.position value, even though the velocity is still acting, so... $$anonymous$$y guess is that it has to do with this, but thanks for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220612125942im_/https://answers.unity.com/themes/thub/images/avi.jpg)