我在 Unity3D 中的检查地面功能不起作用

问题描述 投票:0回答:0

我做了一个小游戏,因为我是 Unity 的初学者,我想添加一个布尔碰撞器来查看我的旋转球是否在地面上以便它跳起来,但它不起作用。 谢谢 !这是代码: `使用系统集合; 使用 System.Collections.Generic; 使用 UnityEngine;

public class script_mouvement : MonoBehaviour
{
    Rigidbody rb;
    [SerializeField] float movementSpeed = 6f;
    [SerializeField] float jumpForce = 5f;

    [SerializeField] Transform groundCheck;
    [SerializeField] LayerMask ground;

    [SerializeField] AudioSource jumpSound;
    


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        rb.velocity = new Vector3(horizontalInput * movementSpeed, rb.velocity.y, verticalInput * movementSpeed);

        if (Input.GetButtonDown("Jump")&& isGrounded())
        {
            Jump();
        }
    }

    void Jump()
    {
        rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
    }

    bool isGrounded(){
    Collider[] colliders = Physics.OverlapSphere(groundCheck.position, 1.0f, ground);
    for (int i = 0; i < colliders.Length; i++) {
        if (colliders[i].gameObject != gameObject) {
            return true;
        }
    }
    return false;
}

}`

我尝试添加碰撞检查但没有成功,我希望球在地面上时会弹起来

c# unity3d
© www.soinside.com 2019 - 2024. All rights reserved.