- Home /
Vector3.forward only moves me in 2 directions when rotating
So the way I am creating my character controller is very different from most mmorpg games. Part of the idea came from Outspark's Fiesta Online, where wasd all moved the character at a degree offset to the camera. For example running with W would move you forward at 0 degrees from the camera while running with D would move you forward at 90 degrees from the camera. This was really nice because you can run towards yourself and easily see all angles of you're character, along with being able to turn with the right mouse button.
For a better idea, this is how I worked it out in code:
float cameraY = cameraController.transform.eulerAngles.y;
float transformY = transform.eulerAngles.y;
if (Input.GetKey("w"))
{
if (transformY != cameraY)
{
transform.eulerAngles = new Vector3(0, cameraY, 0);
playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical"));
}
}
if (Input.GetKey("d"))
{
if (transformY != cameraY + 90)
{
transform.eulerAngles = new Vector3(0, cameraY + 90, 0);
playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Horizontal"));
}
}
if (Input.GetKey("s"))
{
if (transformY != cameraY + 180)
{
transform.eulerAngles = new Vector3(0, cameraY + 180, 0);
playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical"));
}
}
if (Input.GetKey("a"))
{
if (transformY != cameraY + 270)
{
transform.eulerAngles = new Vector3(0, cameraY + 270, 0);
playerController.SimpleMove(movementSpeed * transform.TransformDirection(Vector3.forward) * Input.GetAxis("Horizontal"));
}
}
This works pretty well in the sense that the player stays at an offset to the camera, and that all works great. But for some reason it only moves me in 2 directions. When I press W it moves me forward, but when I press S it still moves me in the same direction. Same with D and A.
Answer by Veldars · Jul 23, 2015 at 06:13 AM
Hi,
I think this is because you rotate your player, you must go forward with the absolut value of "Input.GetAxis" (Mathf.Abs) because the direction will be define by "Vector3.forwarrd".
When you want to go back your forward is back but you multiply it by a negative nuber so your character go in the same direction than with "W"...
I hope this is clear...