C# 为什么我写的跳转代码不适用于Unity3d?

C# 为什么我写的跳转代码不适用于Unity3d?,c#,unity3d,C#,Unity3d,我刚开始用c语言编写代码。我正在用Unity3d做实验。我修复了代码中的错误,不再出现任何错误,但我为跳转我的角色而编写的代码不起作用,我的角色也不跳转。有人能帮我吗 using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { CharacterController controller; Vector3 velocity; Ri

我刚开始用c语言编写代码。我正在用Unity3d做实验。我修复了代码中的错误,不再出现任何错误,但我为跳转我的角色而编写的代码不起作用,我的角色也不跳转。有人能帮我吗

using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    CharacterController controller;
    Vector3 velocity;
    Rigidbody rb;
    bool isGrounded;
    public Transform ground;
    public float distance = 0.3f;
    public float speed;
    public float jump;
    public float jumpHeight;
    public float gravity;
    public LayerMask mask;
    
    private void Start()
    {
        controller = GetComponent<CharacterController>();

    }    
    private void Update()
    {
        #region Movement
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 move = transform.right * horizontal + transform.forward * vertical;
        controller.Move(move*speed * Time.deltaTime);
        #endregion

        #region Jump
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            velocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravity);
        }
        #endregion       

        #region Gravity
        isGrounded = Physics.CheckSphere(ground.position, distance,mask);
        if (isGrounded && velocity.y <0)
        {
            velocity.y = 0f;
        }
        velocity.y += gravity + Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        #endregion
    }
}~~~
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单行为
{
字符控制器;
矢量3速度;
刚体rb;
布尔被禁足;
公共转换场地;
公共浮动距离=0.3f;
公众浮标速度;
公众浮标跳跃;
公众浮标高度;
公众浮力;
公众层面具;
私有void Start()
{
控制器=GetComponent();
}    
私有void更新()
{
#区域运动
float horizontal=Input.GetAxis(“水平”);
float vertical=Input.GetAxis(“垂直”);
Vector3 move=transform.right*水平+transform.forward*垂直;
控制器。移动(移动*速度*时间。延迟时间);
#端区
#区域跳跃
if(Input.GetKeyDown(KeyCode.Space)和&isground)
{
速度y+=数学Sqrt(跳跃高度*-3.0f*重力);
}
#端区
#区域重力
isground=物理。选中球体(地面、位置、距离、遮罩);

如果(isGrounded&&velocity.y看起来您有一个打字错误。在第47行,它可能是

rb.velocity = Vector3.up*Time.deltaTime*jump;

deltaTime后有一个额外的点。

发布代码时,请将其复制粘贴为文本,而不是屏幕截图。
从那里我看到,似乎您缺少了
rb
的声明,顶部应该有一行类似于
刚体rb;
。还要注意的是,您已经将
OnTriggerStay
方法移到了
Update
中,因此它不会像您可能期望的那样被Unity调用。请将它移到
Update
之外>欢迎使用Stack Overflow。请查看以了解如何使用此网站。首先,不要将代码作为图像发布。它是文本,应该以文本的形式包含,并使用“代码”按钮格式化。错误也会出现。请编辑问题并在其中包含必要的信息谢谢您的警告…是的,我是当你说“谢谢”时,我非常感谢你在第一句中的警告。这被认为部分解决了问题。我以不同的方式重写了代码,没有出现错误,但现在我的角色没有跳xD