Visual studio 使用CharacterController跳跃不起作用?

Visual studio 使用CharacterController跳跃不起作用?,visual-studio,unity3d,Visual Studio,Unity3d,我对编码非常陌生,我正在尝试制作FPS,但我无法让跳跃机制工作。我正在学习Brackeys的教程: 我尝试过改变标准的跳跃绑定,我尝试过使用GetKeyDown(KeyCode.Space)而不是使用GetButtonDown(“跳跃”),但似乎没有任何效果。有人知道怎么解决这个问题吗?我爱你。已经挣扎了16个小时了 using System.Collections.Generic; using UnityEngine; public class playermm : MonoBehaviou

我对编码非常陌生,我正在尝试制作FPS,但我无法让跳跃机制工作。我正在学习Brackeys的教程:

我尝试过改变标准的跳跃绑定,我尝试过使用GetKeyDown(KeyCode.Space)而不是使用GetButtonDown(“跳跃”),但似乎没有任何效果。有人知道怎么解决这个问题吗?我爱你。已经挣扎了16个小时了

using System.Collections.Generic;
using UnityEngine;

public class playermm : MonoBehaviour
{    
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
                }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if(Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }
}

使用System.Collections.Generic;
使用UnityEngine;
公共类玩家:单一行为
{    
公共字符控制器;
公共浮子速度=12f;
公共浮子重力=-9.81f;
公共浮子跳线高度=3f;
公开审查;
公共浮地距离=0.4f;
公共层掩码地面掩码;
矢量3速度;
布尔被禁足;
//在第一帧更新之前调用Start
void Start()
{
}
//每帧调用一次更新
无效更新()
{
isground=physical.CheckSphere(groundCheck.position、groundDistance、groundMask);
如果(isGrounded&&velocity.y<0)
{
速度y=-2f;
}
float x=Input.GetAxis(“水平”);
float z=Input.GetAxis(“垂直”);
Vector3 move=transform.right*x+transform.forward*z;
控制器。移动(移动*速度*时间。延迟时间);
if(Input.GetKeyDown(KeyCode.Space)和&isground)
{
速度y=数学Sqrt(跳跃高度*-2f*重力);
}
速度.y+=重力*时间增量;
控制器。移动(速度*时间。增量时间);
}
}

以下是我的建议

在末尾键入此命令:

Debug.Log(isGrounded);
如果角色在地面上,则应打印为true。如果打印为true,则错误在于速度和移动字符或平方根表达式

如果打印错误,则问题在于地面检查。您应该检查“groundCheck”的位置,如果它位于字符的底部,则可以。接下来,检查“接地距离”,如果该值约为0.5,则应该可以。然后检查接地屏蔽。如果地面遮罩与地面应用到的遮罩相同,则可以。如果它返回true,那么其中一个是错误的。 额外帮助的链接:

给我回信