[英]Unity 2D - Jump Animation Reset
我是 Unity 世界 (2D) 的新手,遇到了一些问题。
我有一个脚本,我想在其中跳转。 当我跳跃时,跳跃动画非常快。 您只能在动画师中看到它。
我还有另外两个动画(空闲、运行)可以正常工作。
你能帮我么? :(
public class Player : MonoBehaviour
{
private Rigidbody2D rigid;
[SerializeField]
private float jumpForce = 5.0f;
private bool resetJump;
[SerializeField]
private float speed = 5.0f;
private PlayerAnimation playerAnim;
private SpriteRenderer playerSprite;
// Start is called before the first frame update
void Start()
{
...
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
float move = Input.GetAxisRaw("Horizontal");
Flip(move);
if (IsGrounded())
{
playerAnim.Jump(false);
}
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() == true)
{
rigid.velocity = new Vector2(rigid.velocity.x, jumpForce);
StartCoroutine(ResetJumpNeededRoutine());
playerAnim.Jump(true);
}
rigid.velocity = new Vector2(move * speed, rigid.velocity.y);
playerAnim.Move(move);
}
void Flip(float move)
{
...
}
bool IsGrounded()
{
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.down, 0.3f, 1 << 8);
if (hitInfo.collider != null)
{
if (resetJump == false)
{
return true;
}
}
return false;
}
IEnumerator ResetJumpNeededRoutine()
{
yield return new WaitForSeconds(0.1f);
resetJump = false;
}
}
问题出在你的代码中,在按下空格后,resetjump 变得错误并且 isGrounded() 返回true,因此跳转动画变得错误。 所以我坚持是设置触发器而不是设置 playerAnim.jump 使用 playerAnim.SetTrigger("Jump")。
关于动画触发器的研究我们使用触发器来播放诸如射击和跳跃之类的动画,因为这些动画是真实的,然后在下一瞬间为假。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.