- Home /
The question is answered, right answer was accepted
Still doing Running Animation?
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);
}
}
}
Answer by nikigmi · Nov 26, 2018 at 06:13 PM
Use Input.GetKey(KeyCode.W), Input.GetKey(KeyCode.A), Input.GetKey(KeyCode.S), Input.GetKey(KeyCode.D) instead of the Axis. The axis does not return to 0 immediately after you release W
But i noticed some problems too 1. anim.GetBool("isMove") why do you access the bool from the animator. translation != 0 is the same thing but its alot faster. 2. for these: "isRun", "Forward", "isMove". Use constants!
Try this
public class Movement : MonoBehaviour
{
public const string cstMove = "isMove";
public const string cstForward = "Forward";
public const string cstRunning = "isRun";
static Animator anim;
public float currentSpeed = 2.0f;
public float rotationSpeed = 75.0f;
public float walkSpeed = 2.0f;
public float runSpeed = 6.0f;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
float translation = 0;
float rotation = 0;
if (Input.GetKey(KeyCode.W))
{
translation = 1;
}
else if (Input.GetKey(KeyCode.S))
{
translation = -1;
}
if (Input.GetKey(KeyCode.A))
{
rotation = -1;
}
else if (Input.GetKey(KeyCode.D))
{
rotation = 1;
}
if (translation != 0 && Input.GetKey(KeyCode.LeftShift))
{
currentSpeed = runSpeed;
}
else
{
currentSpeed = walkSpeed;
}
transform.Translate(0, 0, translation * currentSpeed * Time.deltaTime);
transform.Rotate(0, rotation * rotationSpeed * Time.deltaTime, 0);
if (translation != 0)
{
anim.SetBool(cstMove, true);
anim.SetBool(cstForward, translation > 0);
anim.SetBool(cstRunning, translation > 0 && currentSpeed == runSpeed);
}
else
{
anim.SetBool(cstMove, true);
anim.SetBool(cstForward, false);
}
}
}
Ok, thank you very much. There was a flaw with it for walking backward animation, it would always play when I'm idle, I got it down and fixed it by adding another bool for walking backwards and added a new constant. I have never worked with constants, I will do more with them thank you for very much for showing me that.
Constants are better because you make less typing mistakes like "i$$anonymous$$ove" ins$$anonymous$$d of "is$$anonymous$$ove" or "Is$$anonymous$$ove" and they are easier to fix. Also the code looks cleaner and is more readable. And the best is, when you want to change the string, you can rename the variable with the IDE and change the string in only one place.
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers
Disabling script while in certain position 1 Answer
Script is moving player to the wrong position 2 Answers
Jumping from wall to wall Can't jump away from wall to wall 0 Answers