- Home /
Question by
Kderino · Aug 28, 2015 at 11:15 AM ·
rigidbodyplayer movementmoveposition
rigidbody.Moveposition doesnt make my player move diagonally.
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 cant move horizontally and vertically at the same time when using rigibody.MovePosition.
Here is my code for moving the player:
_direction = new Vector3 (Input.GetAxis ("LJoystickHorizontal"), 0, Input.GetAxis ("LJoystickVertical"));
if (_direction.x != 0) {
rigidbody.MovePosition (transform.position + transform.right * _direction.x * _moveSpeed * Time.deltaTime);
}
if (_direction.z != 0) {
rigidbody.MovePosition (transform.position + transform.forward * _direction.z * _moveSpeed * Time.deltaTime);
}
Comment
Answer by prof · Aug 28, 2015 at 06:36 PM
inputDirection = (new Vector3(Input.GetAxis("LJoystickHorizontal"), 0, Input.GetAxis("LJoystickVertical"))).normalized;
Vector3 moveDirection = transform.TransformDirection(inputDirection);
rigidbody.MovePosition (transform.position + moveDirection * _moveSpeed * Time.deltaTime);