- Home /
My controls are inverted when facing sideways.
I just started making a basic 2d top down action game. But already when implementing the basic controls something went wrong. When my character is facing sideways (my move and look keys are seperate) all my movement keys get inverted. Can anyone tell me why this is. My code:
var speed = 2.0;
function Update () {
if(Input.GetKey("w")) {
transform.Translate(transform.up * speed * Time.deltaTime);
animationController.walker.SetBool("walking", true);
}
else if(Input.GetKey("up")) {
transform.rotation.eulerAngles.z = 0;
}
else if(Input.GetKey("a")) {
transform.Translate(-transform.right * speed * Time.deltaTime);
animationController.walker.SetBool("walking", true);
}
else if(Input.GetKey("left")) {
transform.rotation.eulerAngles.z = 90;
}
else if(Input.GetKey("d")) {
transform.Translate(transform.right * speed * Time.deltaTime);
animationController.walker.SetBool("walking", true);
}
else if(Input.GetKey("right")) {
transform.rotation.eulerAngles.z = 270;
}
else if(Input.GetKey("s")) {
transform.Translate(-transform.up * speed * Time.deltaTime);
animationController.walker.SetBool("walking", true);
}
else if(Input.GetKey("down")) {
transform.rotation.eulerAngles.z = 180;
}
else {
animationController.walker.SetBool("walking", false);
}
}
Answer by Pyrian · May 12, 2014 at 04:57 PM
Maybe your "transform.up" and "transform.right" should be "Vector3.up" and "Vector3.right" respectively?
Hey, thanks for the answer. I did that and my controls are no longer inverted, but now the direction in which my character move is based on the direction it is face (e.g. when you are looking left and pressing "w" the character walks to the left), which is not what I want.
You can make your Translates in world rotation ins$$anonymous$$d of local rotation by adding ", Space.World" in their parameters:
transform.Translate(Vector3.up * speed * Time.deltaTime, Space.World);
Yeah that fixed it. Thanks a lot! $$anonymous$$aybe you could help me with another little problem? Currently when I am moving, I can't change the direction I'm looking. So I have to stop, to turn around. Do you know of any way of fixing this?
That's because your single if-else-if structure only allows one input at a time. (I thought it was odd, but refrained from commenting on it.) You'll probably want to separate it into two if-else-if's, the first with all the directions, and the second with the movement.
Glad I could help! :)