简体   繁体   English

我的播放器未编程时正在动画之间切换

[英]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.当我的播放器移动时,当我按住 D 时,animation 在空闲和行走之间切换。我玩了 animation,它看起来不错,但是当我播放过渡时,它开始空闲并切换到行走。 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?不确定我是否从 gif 中正确看到了问题,但你不能关闭“有退出时间”并将转换持续时间更改为 0 吗?

Not sure but using a float instead of a bool to decide when to play the animation might solve this.不确定,但使用浮点数而不是布尔值来决定何时播放 animation 可能会解决这个问题。 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.将条件从空闲到步行更改为Speed greater 0.01并将步行到空闲更改为Speed less 0.01或其他一些较小的数字。

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.更新播放器移动脚本中的 function。

You should also remove the Anim.SetBool() from the funciton animatePlayer() .您还应该从Anim.SetBool()中删除animatePlayer()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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