基本上,我在联合使用Ik系统,我希望我的枪瞄准鼠标。问题是,我只能控制IK骨的位置,这不是子弹的来源,而是一个枪械物体,它是IK骨的一个子物体,它和它一起旋转。我想要做的是以一种方式定位IK,这样FirePoint(子弹就会从这里出来)总是向鼠标旋转。
下面是一个更好地可视化问题的图像

发布于 2020-11-27 17:20:52
评论中的解释
// currentFirePoint and currentIKPoint are the transform of the fire Point and the
// world position of IK Point at some given moment.
// aimOrign and mousePosInWorldSpace are world space positions of the aim Origin and
// mouse position that produce the desired IK point.
Vector3 GetNewIKPoint(Transform currentFirePoint, Vector3 currentIKPoint,
Vector3 aimOrigin, Vector3 mousePosInWorldSpace)
{
float firePointRange = aimOriginToCurrentFP.magnitude;
//Find the local position of IK in Fire Point space
Vector3 curFirePointToIK = currentIKPoint - currentFirePoint;
Vector3 localFirePointToIK = currentFirePoint.InverseTransformVector(
curFirePointToIK);
// Stuff above this point could be done once and cached,
// assuming things like the offset or fire point range don't change.
// Determine where the new fire point will be
Vector3 newFireDirection = (mousePosInWorldSpace - aimOrigin).normalized;
Vector3 newFirePoint = firePointRange * newFireDirection;
// Find rotation such that local right = newFireDirection
// and local up is closer to Vector3.up than Vector3.down
// find local forward
Vector3 newFPLocalForward = Vector3.Cross(newFireDirection, Vector3.up);
// find local up
Vector3 newFPLocalUp = Vector3.Cross(newFPLocalForward, newFireDirection);
Quaternion newFPRotation = Quaternion.LookRotation(newFPLocalForward,
newFPLocalUp);
// Apply that rotation to the local position of IK to find the world space offset.
Vector3 newFirePointToIK = newFPRotation * localFirePointToIK;
// Add to fire point to get world space position
return newFirePoint + newFirePointToIK;
}同样的逻辑也适用于三维空间,但也要相应地改变newFPRotation的计算。
https://stackoverflow.com/questions/65039911
复制相似问题