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

How do I make the player move using their rigid body instead of transform.transalate

Discussion in 'Scripting' started by switchluts, May 23, 2020.

  1. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    Code (CSharp):
    1. private void Move()
    2.     {
    3.         var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
    4.         var newXPos = transform.position.x + deltaX;
    5.         if (hasDied == false)
    6.         {
    7.             transform.position = new Vector2(newXPos, transform.position.y);
    8.         }
    9.         if (IsGrounded() && Input.GetKeyDown(KeyCode.Space) || IsGrounded2() && Input.GetKeyDown(KeyCode.Space) && isTouchingPlayer)
    10.         {
    11.             playerRigid.velocity = Vector2.up * jump;
    12.             forCam.AS.PlayOneShot(jumpClip);
    13.             isTouchingPlayer = false;
    14.         }
    15.  
    16.  
    17.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    For Rigidbody movement either :

    1. let the physics engine move it with forces

    or

    2. do it yourself (like you do above), but you do not set the position; instead you call
    .MovePosition()
    on the Rigidbody instance.

    Both cases give the physics engine a chance to process all collisions and interactions.
     
  3. switchluts

    switchluts

    Joined:
    Mar 18, 2019
    Posts:
    45
    Code (CSharp):
    1.  private void Move()
    2.     {
    3.         movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    4.         if (IsGrounded() && Input.GetKeyDown(KeyCode.Space) || IsGrounded2() && Input.GetKeyDown(KeyCode.Space) && isTouchingPlayer)
    5.         {
    6.             playerRigid.velocity = Vector2.up * jump;
    7.             forCam.AS.PlayOneShot(jumpClip);
    8.             isTouchingPlayer = false;
    9.         }
    10.  
    11.  
    12.     }
    13.     private void FixedUpdate()
    14.     {
    15.         moveCharacter(movement);
    16.     }
    17.     void moveCharacter(Vector2 direc)
    18.     {
    19.         playerRigid.MovePosition((Vector2)transform.position + (direc * moveSpeed * Time.deltaTime));
    20.     }
    21.    
    Why doesn't my player move left or right?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Excellent question!

    May I suggest breaking up lines 3 and 4 and assigning everything to temporary variables, printing each one with Debug.Log() in order to give you a chance to find out?

    You might want to also check that the Rigidbody is not flagged as constrained in some axis (look in the editor inspector).