首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用IK调整目标的Unity3D向量数学问题

利用IK调整目标的Unity3D向量数学问题
EN

Stack Overflow用户
提问于 2020-11-27 15:33:03
回答 1查看 171关注 0票数 0

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

下面是一个更好地可视化问题的图像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-27 17:20:52

评论中的解释

代码语言:javascript
复制
// 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的计算。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65039911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档