首页 > 解决方案 > 如何让玩家在任何表面上跳跃?

问题描述

我已经按照 FPS Character Controller 的 brackeys 教程进行操作,但是如果我将地面(即立方体)替换为地形或其他任何东西,角色将不会跳跃。这是我的 PlayerMovement 代码。


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 float jumpHeight = 3f;

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

    Vector3 velocity;
    bool isGrounded;

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

        velocity.y += gravity * Time.deltaTime;

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

标签: unity3d

解决方案


实际上,要修复它,您需要做的就是将第一人称播放器的地面遮罩设置为所有内容,您应该一切顺利!感谢 BugFinder 提供的帮助。


推荐阅读