如何解决Unity - 在面对目标时添加了额外的偏移量,可能是 Rotate() 与 transform.rotation 之间的区别?
我很困惑使用 Rotate() 和 .rotation 是否会相互冲突。
我正在使用 .rotation 移动变换以自动指向目标,当我选择手动移动时,我使用 Rotate()。但是在我停止手动控制并使用自动模式后,变换将被我所做的手动移动所抵消。所以 Rotate() 正在添加偏移量。手动控制无论什么都是准确的。使用手动控制后,自动定位会增加一个偏移量。
这是手动控制的代码。
xDif = Input.GetAxis("Mouse X") * mouseSensitivity.x;
yDif = -Input.GetAxis("Mouse Y") * mouseSensitivity.y;
float zDif = 0; // <-- gets changed with buttons
transformToMove.Rotate(new Vector3(yDif,xDif,zDif));
/// Returns true if the transform has been successfully oriented to fire at the current target.
private bool TrackTarget(out Vector3 hitPosition,out float hitTime)
{
hitPosition = Vector3.zero;
hitTime = -1.0f;
if (!IsTargetValid(currentTarget))
{
state = TransformState.NoTarget;
return false;
}
if (!GetFiringDirectionToHitTarget(currentTarget,out Vector3 firingDirection,out hitPosition,out hitTime))
{
return false;
}
Quaternion desiredRotation = Quaternion.LookRotation(invertFiringDirection ? -firingDirection : firingDirection);
Quaternion currentRotation = transformToMove.rotation;
float maxDeltaAngle = maxRotationdegreesPerSecond * Time.fixedDeltaTime;
transformToMove2.rotation = Quaternion.Rotatetowards(
currentRotation,desiredRotation,maxDeltaAngle
);
解决方法
听起来您的 desiredRotation
变量受到玩家当前旋转的影响。您的 desiredRotation
是根据 LookRotation()
的 firingDirection
设置的。您的 GetFiringDirectionToHitTarget()
方法是否偶然使用了玩家的当前位置?
基本上这是一个“愚蠢”的错误。在我的手动控制中,我将旋转应用于 transformToMove
,并在编辑器中应用于父变换的子级。因此,如果初始轮换是针对父级 (transformToMove2
),那么显然会有一个偏移量。
抱歉,以后我会留这个帖子教Unity甚至程序员。无论您查看代码并重构多少次,解决方案都可以就在您的眼前。例如,使用错误的变换.....
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。