Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Player not colliding with obstacles properly

Discussion in 'Editor & General Support' started by S3M1CZ, Oct 26, 2019.

  1. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    I'm new to Unity and so far I've created a small experimental game to which I've added a player and a little movement script.
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         Jump();
    4.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    5.         transform.position += movement * Time.deltaTime * moveSpeed;
    6.     }
    The player and terrain have colliders but for some reason the player is not fully stopped when encountering an obstacle. Instead moves a little bit inside the object and starts shaking left and right.

    Is my code too primitive to adress this issue or is there anything else wrong? How can I fix this?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    You are moving your object via transform.position, but colliding via physics.
    transform.position
    does not respect physics collisions. Try using something like Rigidbody.MovePosition.
     
    S3M1CZ likes this.
  3. S3M1CZ

    S3M1CZ

    Joined:
    Feb 11, 2017
    Posts:
    14
    Replaced the
    transform.position
    script with
    Rigidbody.MovePosition
    and it works well now, thanks!
     
    Madgvox likes this.