首页 > 解决方案 > unity 3D 角色 c# 脚本,跳转不起作用?

问题描述

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

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);
    }
}

提前致谢。

标签: c#unity3d

解决方案


推荐阅读