unity3d 我的性格是跳半空中的团结,我不能弄清楚我做错了什么

laik7k3q  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(156)

我正在做一个游戏在团结的基础上,在这个教程https://www.youtube.com/watch?v=rJqP5EesxLk&t=139s,但我的性格不断跳跃半空这里我的代码;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMotor : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool isGrounded;
    public float speed = 5f;
    public float gravity = -9.8f;
    public float jumpHeight = 3f;

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

    // Update is called once per frame
    void Update()
    {
        isGrounded = controller.isGrounded;
    }
    //Receber os inputs e aplicar no controle
    public void ProcessMove(Vector2 input) 
    {
        Vector3 moveDirection = Vector3.zero;
        moveDirection.x = input.x;
        moveDirection.z = input.y;
        controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
        playerVelocity.y += gravity * Time.deltaTime;
        if(isGrounded && playerVelocity.y < 0)
        {
            playerVelocity.y = -2f;
        }
        controller.Move(playerVelocity * Time.deltaTime);
        Debug.Log(playerVelocity.y);

    }
    public void Jump()
    {
        playerVelocity.y = Mathf.Sqrt(jumpHeight * -3.0f * gravity);
    }
}


我尝试创建一个if函数if(isGrounded)

yeotifhr

yeotifhr1#

试试这个:

public bool isGrounded = true;

public void Jump()
{
    if(isGrounded == false) return;
    isGrounded = false;
    
    //Write the jump code here
}

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.tag == "Ground")
        isGrounded = true;
}

相关问题