Clean collisions with walls issue

Hello !

I am facing an issue with a game i’m trying to build in 3D :
My player and my walls have a rigidbody and a box collider. Here is the script in my player object :

public class PlayerMovements : MonoBehaviour
{
    public Rigidbody rb;
    void Start()
    {
    }
    void Update()
    {
        foreach (Touch t in Input.touches) {
            Vector3 translation = new Vector3(t.deltaPosition.x, 0, t.deltaPosition.y) * Time.deltaTime;
            // divide 'translation' by Input.touchCount to remove the speed boost ability
            transform.Translate(translation);
        }
    }

    void OnCollisionEnter(Collision collision) {
        if (collision.gameObject.name == "obstacle")
            rb.velocity = Vector3.zero;
    }
}

As you can see, the player is controlled by finger swipes.
My issue is that when we try to push the player in a wall, it goes slightly into it before moving back. Worst : the player can get completely trough the wall if we swipe fast enough.

I’ve searched trough a lot of topics but i haven’t find any good answer. Does someone have a solution to this ?
Thanks

Hello, and welcome to Unity forums!

You’re moving the position of the player directly and this overrides the physics calculations. In order to the physics to detect and handle collisions properly you must move the rigidbody via Rigidbody.AddForce from FixedUpdate.

Note that AddForce includes a second parameter, ForceMode, which allows to specify the movement not only by applying forces, but also impulses and instant velocity changes.

1 Like

Hello !

Thanks for your answer. My problem is that i want to give translations to my player. When i use rb.MovePosition, even if it is kinematik, i still have the same problem : the player can be “pushed” inside a wall, and it goes back instantly.

Nevermind, i found my answer by using CharacterController.Move instead of RigidBody.MovePosition which works perfectly !