【发布时间】:2018-10-09 15:33:51
【问题描述】:
我正在使用 unity extendedflycam.cs。我添加了一个球体对撞机和一个刚体。目的是避免相机穿过地面和建筑物。这仅在相机以正常速度飞行时实现,但当相机使用 shift 键加速时,如脚本所预期的那样,它会穿过地面和建筑物。这是通过脚本本身的速度倍增器来实现的。 如何避免这种情况?相机在加速时怎么会发生碰撞?
我有没有重力的刚体,质量、阻力和角阻力为 1000。 这是 ExtendedFlycam.cs:
using UnityEngine;
{
public class ExtendedFlycam : MonoBehaviour
{
public float CameraSensitivity = 90;
public float ClimbSpeed = 4;
public float NormalMoveSpeed = 10;
public float SlowMoveFactor = 0.25f;
public float FastMoveFactor = 3;
private float _rotationX;
private float _rotationY;
// ReSharper disable once UnusedMember.Local
private void Start()
{
_rotationX = transform.eulerAngles.y;
}
// ReSharper disable once UnusedMember.Local
private void Update()
{
if (Input.GetMouseButton(1))
{
_rotationX += Input.GetAxis("Mouse X") * CameraSensitivity * Time.deltaTime;
_rotationY += Input.GetAxis("Mouse Y") * CameraSensitivity * Time.deltaTime;
_rotationY = Mathf.Clamp(_rotationY, -90, 90);
}
Quaternion targetRotation = Quaternion.AngleAxis(_rotationX, Vector3.up);
targetRotation *= Quaternion.AngleAxis(_rotationY, Vector3.left);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 4f);
float speedFactor = 1f;
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) speedFactor = FastMoveFactor;
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) speedFactor = SlowMoveFactor;
transform.position += transform.forward * NormalMoveSpeed * speedFactor * Input.GetAxis("Vertical") *
Time.deltaTime;
transform.position += transform.right * NormalMoveSpeed * speedFactor * Input.GetAxis("Horizontal") *
Time.deltaTime;
float upAxis = 0;
if (Input.GetKey(KeyCode.Q)) upAxis = -0.5f;
if (Input.GetKey(KeyCode.E)) upAxis = 0.5f;
transform.position += transform.up * NormalMoveSpeed * speedFactor * upAxis * Time.deltaTime;
}
}
}
【问题讨论】:
-
发布您的代码,如果答案有用,请点赞。
标签: unity3d camera collision fly