- Home /
Isometric WASD Movement using Rigidbodies
I'm trying not to use the Unity CharacterController, and opt for a more flexible system with Rigidbodies.
I'm trying to implement a WASD movement, but how can I match the orientation of the character to the angled Main Camera. I researched a bit and notice someone with the same issue, but the commentors used the CharacterController [in this thread][1].
here's my initial attempt (obviously incorrect):
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 forward = Camera.main.transform.forward;
Vector3 right = Camera.main.transform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
Debug.Log("Forward: " + forward + " Right: " + right);
Vector3 moveDirection = (forward * moveHorizontal) + (right * moveVertical);
transform.forward = moveDirection;
Debug.Log("Facing= " + moveDirection);
rigidbody.velocity = moveDirection * speed;
but I'm not sure how to use those vectors. Any help is appreciated! [1]: http://forum.unity3d.com/threads/182116-Questions-about-Isometric-WASD-Movement
$$anonymous$$aybe you could use Rigidbody.Addforce() for realist movement
Answer by xenonsin · May 10, 2014 at 12:45 PM
void FixedUpdate()
{
var moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveDirection = Camera.main.transform.TransformDirection(moveDirection);
moveDirection.y = 0;
rigidbody.MovePosition(rigidbody.position + moveDirection * speed * Time.deltaTime);
}
Answer by Troas · Aug 10, 2014 at 08:31 PM
Just make the camera a child of the player object. It will translate/rotate with it perfectly.