unity3d - 如何让玩家在任何表面上跳跃?
问题描述
我已经按照 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);
}
}
解决方案
实际上,要修复它,您需要做的就是将第一人称播放器的地面遮罩设置为所有内容,您应该一切顺利!感谢 BugFinder 提供的帮助。
推荐阅读
- python - Python 字符比较
- asp.net-mvc - ASP:ASP 中继器中的文本框在回发后未重新绑定
- css - SCSS 脚本在保存文件时创建嵌套的 css 目录
- arangodb - Foxx 服务:很多还是一个?
- iis - 不断在 webapp 上遇到丢失的包
- javascript - 使用一个 JavaScript 循环将单选按钮值与对象进行比较
- async-await - 为什么这个 xunit 测试死锁(在单个 cpu VM 上)?
- javascript - Mapbox gl js从firebase数据库添加一个标记
- sql - 如何创建创建表以记录错误的触发器?
- python - python忽略成绩计算器中的if语句