首页 > 解决方案 > 为什么我写的跳转代码不能使用 Unity3d?

问题描述

我刚开始用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
    }
}~~~

标签: c#unity3d

解决方案


好像你有错字。在第 47 行,它可能应该是

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

deltaTime 之后有一个额外的点。


推荐阅读