- Home /
how to get walking animation to stop playing
I am trying to get this character to stop playing its animation when you release the w, a, s, or d key but now when I am switching from one key to another the animation won't play. here is the script
void Update()
{
if(Input.GetKeyDown("w"))
{
anim.Play ("BasicMotions@Walk01");
}
if (Input.GetKeyUp("w"))
{
anim.Play ("Idle ");
}
if (Input.GetKeyUp("a"))
{
anim.Play ("Idle ");
}
if (Input.GetKeyUp("s"))
{
anim.Play ("Idle ");
}
if (Input.GetKeyUp("d"))
{
anim.Play ("Idle ");
}
if(Input.GetKeyDown("a"))
{
anim.Play ("BasicMotions@Walk01");
}
if(Input.GetKeyDown("d"))
{
anim.Play ("BasicMotions@Walk01");
}
if(Input.GetKeyDown("s"))
{
anim.Play ("BasicMotions@Walk01");
}
}
} ` can I please have some help, thanks!
Answer by prakyathd801 · Dec 31, 2020 at 05:44 AM
Try replacing your code with below code @Diamondmeh000
anim.Play ("AnimationName", -1, 0);
Answer by OGOUTIS · Dec 26, 2020 at 10:11 AM
This should work
if(Input.GetKey("w") ||Input.GetKey("a") ||Input.GetKey("s") ||Input.GetKey("d"))
{
anim.Play("BasicMotions@Walk01");
}
else
{
anim.Play("Idle");
}
if there is no w,a,s,d input , your character will play idle animation and as you hold down these keys your character will play walk animation
This does not work, the character just zaps to walking and then back to idle.
I have already got a movement script but I am making a seperate one for animations
That's strange.Are you sure you typed Input.GetKey instead of Input.GetKeyDown or anything Because GetKey returns the statement true as long as you press the key but the GetKeyDown returns statement true only on the frame you start pressing.
Answer by $$anonymous$$ · Dec 26, 2020 at 01:26 PM
Use this
private Animator anim;
private void Start (){
// Gets the Animator Component in the gameObject that have this script
anim = GetComponent<Animator>();
}
private void Update (){
// Gets the Input of the w,a,s,d/up,left,right,down arrows
Vector2 moveInput = new Vector2 (Input.GetAxisRaw(Horizontal),.
(Input.GetAxisRaw(Vertical);
// Make a float parameter called "Speed" In the Animator Component
anim.SetFloat("Speed", moveInput.magnitude);
// If you do not know how to use the Animator Component post a
comment
}
Answer by Diamondmeh000 · Jan 02, 2021 at 07:00 AM
this answer is not really an answer I just needed the code to be formatted properly. I realised that I did type Input.GetKeyDown instead of Input.GetKey and that fixed the problem but now my movement script is not working, when I press W, a, s, or d it zaps to a random position here it is
public class ThirdPersonScript : MonoBehaviour {
public Transform cam;
public CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.15f;
float turnSmoothVelocity;
void Start ()
{ Cursor.visible = false; Screen.lockCursor = true; }
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 movedirection = Quaternion.Euler(0f, targetAngle, 0f) *Vector3.forward;
controller.Move(movedirection.normalized * speed * Time.deltaTime);
}
}
} ` can I have some help please because this is extremely annoying any help would be appreciated
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Inheritance in C# and Unity3d 1 Answer
Mouse targeting error 1 Answer
How do I seed the random number generator myself and still get pseudorandom values? 2 Answers