- Home /
How to combine movements of the transform.rotation and transform.position
So, I'm wondering what to do for a code like this in C#:
using System.Collections;
public class Movement : MonoBehaviour {
public float speed = 0.45f;
public float turnSpeed = 20f;
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.UpArrow)){
animation.CrossFade ("Walk");
transform.position += transform.forward * speed;
}
else if (Input.GetKey(KeyCode.RightArrow)){
animation.CrossFade ("Walk");
transform.Rotate(Vector3.up * turnSpeed);
}
else if (Input.GetKey(KeyCode.LeftArrow)){
animation.CrossFade ("Walk");
transform.Rotate(Vector3.down * turnSpeed);
}
else{
animation.CrossFade("Idle");
}
}
}
So what do I do if I want to combine the movements of the rotations and the forward transform.position? For example, when I press the up arrow, the character walks forward, and when I press one of the right/left arrows the character turns around, however if I press the up arrow and the left/right arrows at the same time, only one happens (it either turns in place or walks forward)
So my question is, how do I code it so the character can walk forward while turning, and later walk backwards while turning, doing both at the same time without having to continuously just press the side arrows and the up arrows separately? This is so I can have smoother controls, obviously.
Thanks for reading!
Just use a bunch of if statements ins$$anonymous$$d of if/else if/else
What do you mean? I used to work with JS, so I'm newer to C#. In Java, you had to always add an 'else' or you couldn't add multiple 'ifs'.
So if you just use 'if' one doesn't override the other?
Answer by Bunny83 · Oct 23, 2014 at 02:06 AM
Uhm, just remove the "else" from your if statements so each if statement is checked regardless of the one before. However you have to handle your pure else differently:
void Update()
{
bool moving = false;
if (Input.GetKey(KeyCode.UpArrow)){
moving = true;
animation.CrossFade ("Walk");
transform.position += transform.forward * speed;
}
if (Input.GetKey(KeyCode.RightArrow)){
moving = true;
animation.CrossFade ("Walk");
transform.Rotate(Vector3.up * turnSpeed);
}
if (Input.GetKey(KeyCode.LeftArrow)){
moving = true;
animation.CrossFade ("Walk");
transform.Rotate(Vector3.down * turnSpeed);
}
if (!moving)
{
animation.CrossFade("Idle");
}
}
It almost works, however the walk animation finishes, freezes, and idle doesn't play again. It only plays at the start.
Are you sure you have the moving boolean declared inside Update? What you describe would happen when you declare it as class member variable which it shouldn't be.
I'll edit my answer to make that more clear ;)
The variable (which is just a local variable on the stack) is always initialized as false. If no movement happens it will be still false when you reach the if(!moving)
statement. However if any (or multiple) of your other if statements execute it will be set to true.
Ahhh yes it's a variable, not in Update! Thanks! I'll try it out. :)
edit -- It worked, thanks!