【发布时间】:2014-09-02 08:51:51
【问题描述】:
我有一个奇怪的故障,当我从角落走下来(我不按跳跃键)时,玩家会以非常快的速度摔倒。如果我跳,那么一切都会正常。 (它的 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);
}
}
【问题讨论】:
-
你确定这是你看到的异常吗?我的意思是,站立坠落比跳跃更快达到给定的向下速度,这就是运动学的工作原理。
-
是的,如果我不跳,就从楼上下来,就像传送到地板一样。
-
好的,所以我不是 Unity 开发人员,我不确定这一点,但问题可能是 y 位置是相对于当前角色下方的地面高度测量的?因此,当您离开窗台时,您的角色实际上会保持一个恒定的 y 位置,并且没有发生加速度,只是 y=0 的位置移动得更低了。
-
这取决于你如何实现
CharacterController我想,特别是Move方法。 -
这是一个很好的猜测 Asad,替代方法可能是您的 y 速度/加速度非常高,但控制器代码阻止您从地面坠落。当您从壁架上走下来时,由于速度快,几乎是瞬间坠落。取决于你是否实现了终端速度以及坠落的距离(你应该能够看到一些中间帧,即如果坠落足够远,则不是完全瞬时的)。