这是我的代码和检查器数据,我是新手,我不知道为什么它不工作。我将其标记为 3D 问题,因为我认为它在任何情况下都不起作用。我看了一些 视频和其他帖子,我找不到错误。
This is picture of my inspectorpublic Animator animator;
public SpriteRenderer sprite;
public Rigidbody2D rb;
Vector2 playerDirection;
public float speed;
public float jumpForce;
public float fallMultiplier=2.5f;
public float lowJumpMultiplier=2f;
void Update()
{
playerDirection.x = Input.GetAxisRaw("Horizontal");
//Animation
////////////////////////////////////////////////////
if (playerDirection.x != 0)
animator.SetBool("isMoving", true);
else
animator.SetBool("isMoving", false);
////////////////////////////////////////////////////
//flip
////////////////////////
if (playerDirection.x < 0)
{
sprite.flipX = true;
}
if (playerDirection.x>0)
{
sprite.flipX = false;
}
////////////////////////
//Jump
///////////////////////
////////////////////////
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);
if (Input.GetButton("Jump"))
{
rb.velocity = Vector2.up * jumpForce;
}
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * fallMultiplier * Time.fixedDeltaTime;
}
else
if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.W))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - rb.gravityScale) * Time.fixedDeltaTime;
}
}

首先你需要了解 Update 和 FixedUpdate 之间的区别。更新在所有帧上运行。FixedUpdate 以特定的时间间隔运行。你正在做的是在 FixedUpdate 中注册用户输入。这意味着,你需要幸运,让 FixedUpdate 在你按下按钮的特定帧上运行。
试试这个:
void Update()
{
playerDirection.x = Input.GetAxisRaw("Horizontal");
//Animation
////////////////////////////////////////////////////
if (playerDirection.x != 0)
animator.SetBool("isMoving", true);
else
animator.SetBool("isMoving", false);
////////////////////////////////////////////////////
//flip
////////////////////////
if (playerDirection.x < 0)
{
sprite.flipX = true;
}
if (playerDirection.x>0)
{
sprite.flipX = false;
}
////////////////////////
//Jump
///////////////////////
if (Input.GetButton("Jump"))
{
Debug.Log("Jumping on registered");
rb.velocity = Vector2.up * jumpForce;
}
////////////////////////
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + speed *playerDirection * Time.fixedDeltaTime);
if (rb.velocity.y < 0)
{
rb.velocity += Vector2.up * fallMultiplier * Time.fixedDeltaTime;
}
else
if (rb.velocity.y > 0 && !Input.GetKey(KeyCode.W))
{
rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - rb.gravityScale) * Time.fixedDeltaTime;
}
}
一旦你确保跳转注册,它应该可能工作正常。但是,如果你按下跳转按钮时没有得到控制台日志,你应该检查你的输入设置。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(43条)