首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一-将两个局部旋转应用于对象(试图将控制器操纵杆的旋转重新创建为三维网格操纵杆)

问统一-将两个局部旋转应用于对象(试图将控制器操纵杆的旋转重新创建为三维网格操纵杆)
EN

Stack Overflow用户
提问于 2020-12-08 20:21:37
回答 2查看 136关注 0票数 0

我想要重新一对一,旋转的现实生活控制器操纵杆(即360控制器)为一个三维操纵杆网格(类似360控制器之一)。

我想通过根据输入的大小旋转X轴上的操纵杆(将它映射到X轴中的最小旋转和最大旋转)来实现。然后绘制输入的角度,并将其应用到3D操纵杆的Y轴上。

这是我的代码,操纵杆在X轴上正确倾斜,但Y轴的旋转不起作用:

代码语言:javascript
复制
    public void SetStickRotation(Vector2 stickInput)
{
    float magnitude = stickInput.magnitude;

    // This function converts the magnitude to a range between the min and max rotation I want to apply to the 3D stick in the X axis
    float rotationX = Utils.ConvertRange(0.0f, 1.0f, m_StickRotationMinX, m_StickRotationMaxX, magnitude);
    
    float angle = Mathf.Atan2(stickInput.x, stickInput.y);
    
    // I try to apply both rotations to the 3D model
    m_Stick.localEulerAngles = new Vector3(rotationX, angle, 0.0f);
}

我不知道为什么不工作,即使我这样做是正确的(也就是说,也许有一个更好的方法来实现它)。

非常感谢您的投入。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-08 22:25:24

我建议旋转它的数量,由星等决定,围绕一个单一的轴确定的方向。这将避免操纵杆旋转,在不对称操纵杆(如飞行员操纵杆)的情况下尤为明显:

​

​

评论中的解释:

代码语言:javascript
复制
public void SetStickRotation(Vector2 stickInput)
{
    /////////////////////////////////////////
    // CONSTANTS (consider making a field) //
    /////////////////////////////////////////

    float maxRotation = 35f; // can rotate 35 degrees from neutral position (up)

    ///////////
    // LOGIC //
    ///////////

    // Convert input to x/z plane
    Vector3 stickInput3 = new Vector3(stickInput.x, 0f, stickInput.y);

    // determine axis of rotation to produce that direction
    Vector3 axisOfRotation = Vector3.Cross(Vector3.up, stickInput3);

    // determine angle of rotation
    float angleOfRotation = maxRotation * Mathf.Min(1f, stickInput.magnitude);

    // apply that rotation to the joystick as a local rotation
    transform.localRotation = Quaternion.AngleAxis(angleOfRotation, axisOfRotation);
}

这将适用于操纵杆:

and

  • stickInput
  1. 从它的轴到它的末端的方向是局部向上的方向,
  2. 它应该在中性输入上有零的(恒等)旋转,y=0的y=0应该围绕棍子的前/后轴旋转旋钮,用x=0的stickInput应该围绕棍子的左右轴旋转旋钮。

​

​

票数 1
EN

Stack Overflow用户

发布于 2020-12-09 16:46:42

找出问题,atan2返回辐射角,但是代码假设它是欧拉度,当我完成转换时,它工作得很好。如果有人感兴趣,我将代码放在这里(而不是atan2函数中的更改):

代码语言:javascript
复制
public void SetStickRotation(Vector2 stickInput)
{
    float magnitude = stickInput.magnitude;

    // This function converts the magnitude to a range between the min and max rotation I want to apply to the 3D stick in the X axis
    float rotationX = Utils.ConvertRange(0.0f, 1.0f, m_StickRotationMinX, m_StickRotationMaxX, magnitude);
    
    float angle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg;
    
    // Apply both rotations to the 3D model
    m_Stick.localEulerAngles = new Vector3(rotationX, angle, 0.0f);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65206320

复制
相关文章

相似问题

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