我有一个奇怪的故障,当我从一个角落走时(我不按跳跃),玩家将以非常快的速度跌倒。如果我跳,那么一切都正常。 (它的Quill18 FPS控制器,我从那里学习,这就是为什么我不使用内置控制器)
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour
{
public float movementSpeed = 5.0f;
public float mouseSensitivity = 5.0f;
public float jumpSpeed = 20.0f;
float verticalRotation = 0;
public float upDownRange = 60.0f;
float verticalVelocity = 0;
CharacterController characterController;
// Use this for initialization
void Start()
{
// Screen.lockCursor = true;
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
// Rotation
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
// Movement
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if (characterController.isGrounded && Input.GetButton("Jump"))
{
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed ;
speed = transform.rotation * speed;
characterController.Move(speed * Time.deltaTime);
}
}
答案 0 :(得分:1)
问题是你在这一行上运行的每一帧:
verticalVelocity + = Physics.gravity.y * Time.deltaTime;
因此,你获得了动力&#34;每一秒,它都不会停止,直到你跳,因为你重新&#34;重置&#34; Y速度为正常值。我之前遇到过这个问题,当你没有接地时,只需添加Y速度就可以解决这个问题。您可以使用Raycast检查您是否有地面,如果不这样做,请将该数量增加verticalVelocity
。