Search Unity

rigidbody.Moveposition doesnt make my player move diagonally.

Discussion in 'Physics' started by Kderino, Aug 28, 2015.

  1. Kderino

    Kderino

    Joined:
    Aug 28, 2015
    Posts:
    1
    I want my player to move using rigidbodies and this is the last thing i know that comes good with jumping. But my problem is, it seems like you can't move horizontally and vertically at the same time when using rigibody.MovePosition.

    Here is my code for moving the player:
    Code (CSharp):
    1. _direction = new Vector3 (Input.GetAxis ("LJoystickHorizontal"), 0, Input.GetAxis ("LJoystickVertical"));
    2.         if (_direction.x != 0) {
    3.             rigidbody.MovePosition (transform.position + transform.right * _direction.x * _moveSpeed * Time.deltaTime);
    4.         }
    5.         if (_direction.z != 0) {
    6.             rigidbody.MovePosition (transform.position + transform.forward * _direction.z * _moveSpeed * Time.deltaTime);
    7.         }
     
  2. nporaMep

    nporaMep

    Joined:
    Oct 31, 2014
    Posts:
    33
    You must make 1 MovePosition call per FixedUpdate, because it doesn't happen as soon as you call MovePosition. Physics are processed after FixedUpdate.
    Combine transform.right * _direction.x and transform.forward * _direction.z in single vector, then pass it to MovePosition.
     
  3. bradywestveer

    bradywestveer

    Joined:
    Mar 18, 2015
    Posts:
    6
    How?
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Use a Vector3 and pass that in. It's not rocket science. Stop doing maths in function calls. Construct the Vector3 first and then modify that instead, before finally passing it once to moveposition.
     
  5. nporaMep

    nporaMep

    Joined:
    Oct 31, 2014
    Posts:
    33
    var myResultingVector = transform.right* _direction.x* _moveSpeed *Time.deltaTime
    + transform.forward* _direction.z* _moveSpeed *Time.deltaTime

    rigidBody.MovePosition(transform.position + myResultingVector)