So I’m stuck trying to make a movement script for my character and I’m trying to make the sprinting part of it and it does some weird things. When I press “W” is does the walking animation just fine and it kind of doesn’t automatically go into and out of the sprint animation but that is probably just an animation problem and I can fix it later. The problem lies when I try to come out of the sprint, when I am holding down “W” and “Left Shift” it sprints but if I let go of “W” but still hold “Left Shift” the animation still plays and even after I let go of “Left Shift”. Here is the code:
public class Movement : MonoBehaviour {
static Animator anim;
public float speed = 2.0f;
public float rotationSpeed = 75.0f;
public float runSpeed = 6.0f;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
//Vertical Movements
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
if(translation > 0) {
anim.SetBool("Forward", true);
anim.SetBool("isMove", true);
}
if(translation == 0) {
anim.SetBool("Forward", false);
anim.SetBool("isMove", false);
}
if(translation < 0) {
anim.SetBool("Forward", false);
anim.SetBool("isMove", true);
}
//Running
if(Input.GetKey(KeyCode.LeftShift) && anim.GetBool("isMove") == true) {
//Speed Controller
speed = runSpeed;
//Animations
anim.SetBool("Forward", true);
anim.SetBool("isRun", true);
} else {
//Walking Speed
speed = 2.0f;
//Animation return
anim.SetBool("isRun", false);
}
}
}