简体   繁体   中英

My player is switching between animations when it is not programed to

When my player is moving, the animation switches between idle and walk when I am holding down D. I played the animation and it looks fine but when I play the transition it starts idle and switches to walk. I have tried creating a new project and doing it again but it does the same thing. Does anyone know how to fix this?

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player: MonoBehaviour { [SerializeField] float moveForce = 10f; [SerializeField] float jumpForce = 11f; float movementX; Rigidbody2D myBody; Animator Anim; private SpriteRenderer sr; string WALK_ANIMATION = "Walk"; private void Update() { playerMoveKeyboard(); animatePlayer(); } private void Awake() { myBody = GetComponent<Rigidbody2D>(); Anim = myBody.GetComponent<Animator>(); sr = GetComponent<SpriteRenderer>(); } void playerMoveKeyboard() { movementX = Input.GetAxisRaw("Horizontal"); transform.position += new Vector3(movementX, 0f, 0f) * moveForce * Time.deltaTime; } void animatePlayer() { if(movementX > 0) { Anim.SetBool(WALK_ANIMATION, true); sr.flipX = false; } else if (movementX < 0) { Anim.SetBool(WALK_ANIMATION, true); sr.flipX = true; } else { Anim.SetBool(WALK_ANIMATION, false); } } }

我的动画

动画窗口

Not sure if I am seeing the problem correctly from the gif, but can't you just turn off "Has exit time" and change the transition duration to 0?

Not sure but using a float instead of a bool to decide when to play the animation might solve this. That is how I've been doing it and never had any problems.

Do this by creating a new parameter in the animator called "Speed". Change the condition from idle to walk to Speed greater 0.01 and walk to idle to Speed less 0.01 or some other small number.

In the code you can change "Speed" to the player speed by adding:

 anim.setFloat("Speed", Mathf.Abs(movementX))

to the update function inside the player movement script.

You should also remove the Anim.SetBool() from the funciton animatePlayer() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM