Unity 3D角色c#脚本,跳转不起作用?

Unity 3D角色c#脚本,跳转不起作用?,c#,unity3d,C#,Unity3d,我有一个第一人称的玩家控制器c#脚本,(我遵循了brackeys的教程),我对跳跃输入进行了编码,基本上是逐字逐句的,但由于某种原因它不会跳跃,而是一旦我开始游戏,玩家就会无限上浮,这绝对不是我想要的。以下是脚本,以防你们中的任何人知道如何修复它(如果错误是打字错误或其他什么,很抱歉,但这将非常有帮助): 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类玩家运动:单一行为 { 公共字符控制器; 公共浮子速度=12f; 公共浮子重力=

我有一个第一人称的玩家控制器c#脚本,(我遵循了brackeys的教程),我对跳跃输入进行了编码,基本上是逐字逐句的,但由于某种原因它不会跳跃,而是一旦我开始游戏,玩家就会无限上浮,这绝对不是我想要的。以下是脚本,以防你们中的任何人知道如何修复它(如果错误是打字错误或其他什么,很抱歉,但这将非常有帮助):

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家运动:单一行为
{
公共字符控制器;
公共浮子速度=12f;
公共浮子重力=-9.81f;
公共层掩码地面掩码;
公共浮地距离=0.4f;
公开审查;
公共浮子跳线高度=3f;
矢量3速度;
布尔被禁足;
无效更新()
{
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.GetButtonDown(“跳转”)和&isground)
{
速度y=数学Sqrt(跳跃高度*-2f*重力);
}
速度.y+=重力*时间增量;
控制器。移动(速度*时间。增量时间);
}
}

提前感谢。

您的代码与此处的代码非常相似:也许这是一个愚蠢的问题,但您的PlayerMovement脚本是否已附加到游戏对象?问题可能不在于代码,而在于场景,无论是忘记附加脚本还是忘记在编辑器中定义变量。测试这个假设的一种方法是复制粘贴在该链接上的代码到控制器中,看看是否得到相同的行为。函数<代码> UpDebug()/Ungult >非常重要,并考虑编写代码。您是否尝试检测跳转并将其交给函数,请在更新->调用函数Move()中跳转触发器。为了澄清,我认为您需要了解更多关于Brackey教程的内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public LayerMask groundMask;

    public float groundDistance = 0.4f;
    public Transform groundCheck;

    public float jumpHeight = 3f;

    Vector3 velocity;
    bool isGrounded;
    
    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.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;

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