- Home /
Question by
xylaphonian · Sep 15, 2019 at 09:09 PM ·
rotationmovementdirection
How to make a movement system that changes with your viewpoint but also lets you hit multiple keys at once to go diagonally?
Just to make it clear I am not very good with unity or C# so my apologies if I say something dumb!
So I started out by making a system of movement like so:
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
rb.velocity = (new Vector3(moveHorizontal * movementSpeed * Time.deltaTime, 0, moveVertical * movementSpeed * Time.deltaTime));
(Where rb is the Rigidbody component and movementSpeed is a changable public float).
However when you turn the camera to look in a different direction the direction of "forwards" does not also change it remains constant so I tried making another system of movement like so:
if (Input.GetKey(KeyCode.W))
{
rb.velocity += transform.forward * movementSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.S))
{
rb.velocity -= transform.forward * movementSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D))
{
rb.velocity += transform.right * movementSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.A))
{
rb.velocity -= transform.right * movementSpeed * Time.deltaTime;
}
However in this system you are unable to hit two buttons at once, to go diagonally, without hard coding every combination of buttons.
So my question is, is there a system where I can make diagonal movement possible while also changing the direction of forwards (and other directions) based on the rotation of the object?
Comment