我已经制作了一个等距角色控制器,以便它可以在基于等距的透视图中移动并前进。问题是,当我尝试使用2个键(W + D,A + S ...)移动并释放这些键时,玩家倾向于面对最后释放的键,因此很难朝对角线方向移动方向。这使角色在释放键的最后一刻翻转其旋转。
我一直在考虑使用一种睡眠或协程检查是否在短时间内按下并释放了两个键,只是不旋转它。
有没有解决这个问题的方法?
这是我的代码(我刚刚从这里复制了它:https://www.studica.com/blog/isometric-camera-unity)
private void SetIsometricDirections() {
vertical = Camera.main.transform.forward;
vertical.y = 0;
vertical = Vector3.Normalize(vertical);
horizontal = Quaternion.Euler(new Vector3(0, 90, 0)) * vertical;
}
private void Move() {
Vector3 horizontalMovement = horizontal * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
Vector3 verticalMovement = vertical * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");
Vector3 heading = Vector3.Normalize(horizontalMovement + verticalMovement);
Heading(heading);
transform.position += (horizontalMovement + verticalMovement);
}
private void Heading(Vector3 heading) {
transform.forward = heading;
}
很明显,它是“ Heading”方法中的标题技巧。
答案 0 :(得分:0)
我终于找到了解决方案。 我将其发布,以防万一将来有人需要它。
我根据运动来计算当前旋转。
private void SetRotation() {
currentRotation = Quaternion.LookRotation(movement);
}
然后,我创建了第二个四元数,以存储与当前旋转不同的最后一个旋转。我还使用计数器存储停止运动或更改方向之前面对旋转的时间。
private void CheckSameRotation() {
if (lastRotation != currentRotation || movement == Vector3.zero) {
sameRotationTime = 0;
lastRotation = currentRotation;
}
else {
sameRotationTime += Time.deltaTime;
}
}
然后,我用bool来检查时间是否足够长以使旋转发生。
private void TimeAtSameRotation() {
canRotate = (sameRotationTime < 0.015f) ? false : true;
}
然后,如果运动不为零并且条件“ canRotate”为true,则最终旋转对象。
private void Rotate() {
if (movement != Vector3.zero && canRotate) {
transform.rotation = currentRotation;
}
}