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 *);*
-
}*
-
// 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!