【发布时间】:2021-04-04 06:07:52
【问题描述】:
所以我才刚接触 Unit 2 天,我遇到了我的玩家在空中跳跃的问题。我可以继续点击空格键,我的玩家会继续跳跃而不是停止,我正在努力做到这一点,所以玩家只能在地面上而不是空中跳跃。我一直在尝试很多教程,其中大多数只是让我的游戏冻结并且不再工作。我也一直在尝试找出如何使用地面检查,但我仍然不确定如何做到这一点。
这是我知道的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PM : MonoBehaviour
{
public float moveSpeed;
public float jumpHeight;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update() {
float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
rb.velocity = new Vector2(moveDir, rb.velocity.y);
// Your jump code:
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
}
}
}
【问题讨论】: