unity3d - unity fps 跳跃与角色控制器
问题描述
我对统一很陌生,想创建一个 fps 运动。我从 Brackeys 找到了本教程,它的效果相对较好,但如果你在头顶上方跳起天花板,你会飞一会儿。很抱歉提出这样一个基本问题,但我找不到任何东西。这里有一些代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
Animator animator;
public float speed = 7f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void Start()
{
animator = GetComponent<Animator>();
}
// 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");
if (x != 0)
{
animator.SetBool("WalkXTrigger", true);
x /= 2;
}
else
{
animator.SetBool("WalkXTrigger", false);
}
if (z != 0)
{
if (z<0)
{
animator.SetBool("WalkBackTrigger", true);
z /= 1.5f;
}
if (z>0)
{
animator.SetBool("WalkTrigger", true);
}
}
else
{
animator.SetBool("WalkTrigger", false);
animator.SetBool("WalkBackTrigger", false);
}
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);
animator.SetBool("JumpTrigger", true);
}
else
{
animator.SetBool("JumpTrigger", false);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
所以我认为有2个选择:1.当他撞到天花板时将角色向下移动2.测量角色和天花板之间的空间并将其设置为等于跳跃高度我的问题:我不知道该怎么做希望我的英语没问题,问题描述得足够多谢谢你的帮助
解决方案
我不太清楚为什么,但这是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour {
public CharacterController controller;
public float speed = 12f;
public float speedy;
public float speedOffset = 3f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float playerHeight = 1.6f;
public float groundDistance = 0.4f;
Vector3 velocity;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Ray downRay = new Ray(transform.position, -Vector3.up);
if (Physics.Raycast(downRay, out hit))
{
if (hit.distance >= playerHeight + groundDistance)
{
isGrounded = false;
}
else
{
isGrounded = true;
}
}
if (isGrounded)
{
speedy = speed/(1 + speedOffset);
}
else
{
speedy = speed;
}
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 * speedy * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
我不知道,我是一个没有经验的9岁的孩子,所以试试这个看看它是否有效
推荐阅读
- awk - 如何让“awk”只修改文件的某些行,而不是整个文件?
- android - Nativescript 在构建期间无法删除文件
- javascript - 如何将Javascript代码插入R中的Leaflet,即使双击也不会触发点击
- javascript - 为什么代码可以在 JSfiddle 上运行,但不能在我的 PC 上运行?(滚动)
- google-sheets - 我可以一次保护范围 2 或 3 单元格吗?
- pytest - Pytest测试是方法抽象
- azure-notificationhub - 格式化从 Azure 通知中心发送的通知
- typescript - 定义可以具有不同结构的对象参数的类型
- karate - 匹配输入和输出的空手道问题
- spring - 如何将未经 Envers 审计的字段添加到审计表