求助!我正在尝试统一创建我的第一个 2D 平台游戏,但我的脚本无法正常工作。我设计脚本的目的是让我只能跳跃一次,而不能通过反复按空格键来飞行。我不知道为什么我的代码不起作用。我的 raycast 正在检测平台,因为我可以在控制台中看到调试消息,但它没有返回任何内容。
这是我目前的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
`public class Player_Jump : MonoBehaviour`
{
[SerializeField] private LayerMask layerMask;
public Rigidbody2D rb;
private float jumpForce = 6f;
private BoxCollider2D boxCollider2d;`
`// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();`
`boxCollider2d = transform.GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update()`
`{
if (IsGrounded() && Input.GetButtonDown("Jump"))
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
private bool IsGrounded()`
`{
RaycastHit2D raycastHit2d = Physics2D.BoxCast(boxCollider2d.bounds.center, boxCollider2d.bounds.size, 0, Vector2.down * .1f, layerMask);
return raycastHit2d.collider != null;
}
}