【发布时间】:2019-03-10 15:18:33
【问题描述】:
我是 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;
}
}
【问题讨论】:
-
我建议您在查看脚本之前先查看动画师和动画。检查是否:a)动画速度(默认为 1) b)动画状态有一个“有退出时间”,您可以尝试取消选中它,这样动画会在更改状态之前结束。 c) 对动画使用触发器而不是布尔值(将默认设置为空闲并在跳跃时触发动画)。您的代码似乎没有任何问题,所以我想这与动画师或动画有关。
-
非常感谢您的回答。我再次检查了你提到的几点。一种。速度 = 1;我已经更改并测试过,没有更改 b.有退出时间=禁用,我也试过有无,没有变化c。改为触发;没有变化 你能想到其他选择吗?我可以上传便于错误分析的信息吗?