当没有编程到动画时,我的播放器正在调整动画
当我的播放器移动时,当我按住D时,动画会在空闲和步行之间切换D。我演奏了动画,但是当我播放过渡时,它会开始闲置并切换到步行。我尝试创建一个新项目并再次进行,但是它也做同样的事情。有人知道如何解决这个问题吗?
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);
}
}
}
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
不确定我是否从GIF中正确看到了问题,但是您不能仅关闭“有退出时间”并将过渡持续时间更改为0?
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?
不确定,但是使用浮子而不是布尔来决定何时播放动画可以解决此问题。这就是我一直在做的事情,从来没有遇到任何问题。
通过在动画仪中创建一个名为“ speed”的新参数来执行此操作。将条件从空闲到步行到
速度更大0.01
,然后步行到闲置>速度少0.01
或其他一些小数。在代码中,您可以通过添加:
在播放器移动脚本中的更新功能中将“速度”更改为播放器速度。
您还应从Funciton
AnimatePlayer()
中删除Anim.SetBool()
。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 toSpeed less 0.01
or some other small number.In the code you can change "Speed" to the player speed by adding:
to the update function inside the player movement script.
You should also remove the
Anim.SetBool()
from the funcitonanimatePlayer()
.