Object doesn't collide with wall using transform.translate

public class PlayerMovement : MonoBehaviour
{
private float m_distanceTraveled = 0f;
private float distance = 74f;

    private void Start()
    {
    }
    void Update()
    {
        Move();
    }

    private void Move()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            if (m_distanceTraveled < distance)
            {
                Vector3 oldPosition = transform.position;
                transform.Translate(0, 0, distance);
                m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
                m_distanceTraveled = 0f;
            }
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            if (m_distanceTraveled < distance)
            {
                Vector3 oldPosition = transform.position;
                transform.Translate(0, 0, -distance);
                m_distanceTraveled = Vector3.Distance(oldPosition, transform.position);
                m_distanceTraveled = 0f;
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            if (m_distanceTraveled < distance)
            {
                Vector3 oldPosition = transform.position;
                transform.Translate(distance, 0, 0);
                m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
                m_distanceTraveled = 0f;
            }
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (m_distanceTraveled < distance)
            {
                Vector3 oldPosition = transform.position;
                transform.Translate(-distance, 0, 0);
                m_distanceTraveled += Vector3.Distance(oldPosition, transform.position);
                m_distanceTraveled = 0f;
            }
        }
    }
}

This is my code.
I need to move object on certain distance by pressing key once.I have Box Colliders and RigidBodies(Collision detection - Continuous Dynamic) put on walls and charachter.Thanks in advance!

If you are working with collisions, you need to align your code with the physics engine in Unity.


First of all, when moving an object that needs to detect collisions, you should move it in FixedUpdate(). Fixed update is a loop separate from the update loop and it is the loop where all physics calculations are made. It runs at intervals that you can determine (Time.fixedDeltaTime is the time in seconds that FixedUpdate() runs).


The other issue is that, although you have a rigidbody on your object, you are not using the rigidbody to apply forces for the movement. Unity - Scripting API: Rigidbody.AddForce Take a look at that link. The following is how to apply forces to your rigidbodies: rigidbody.AddForce(normalizedDirectionVector * forceToApply)


AddForce() can be a bit difficult to control if you just want to move the rigidbody and control the exact speed and have it stop at an exact position for example. This is where you could opt for directly setting the velocity vector of the rigidbody, but this is not suggested if you are looking for realistic movements, so I would only use it for mechanical equipment, for example.

@danylovyslo2003 this should solve your problem.

Use the RigdBody velocity, the translate can “teleport” near colliders.

This code solve your problem:

 void move()
    {
        if(Input.GetKeyDown(KeyCode.RightArrow) && m_distanceTraveled < _distance) 
           {
               oldposition = transform.position;
             _playerRB.velocity = Vector3.right * vel;
           }

        // Put Here the other keys. But i recommend you to use the GetAxis...


        // The controls to calculate the move and the stop conditions.
        if (_playerRB.velocity != Vector2.zero)
        {
            m_distanceTraveled += Vector2.Distance(oldposition,transform.position);
        } 

        if (m_distanceTraveled >= _distance) 
        {
           _playerRB.velocity = Vector2.zero;
           m_distanceTraveled = 0f;
        } 
       
    }