我想要重新一对一,旋转的现实生活控制器操纵杆(即360控制器)为一个三维操纵杆网格(类似360控制器之一)。
我想通过根据输入的大小旋转X轴上的操纵杆(将它映射到X轴中的最小旋转和最大旋转)来实现。然后绘制输入的角度,并将其应用到3D操纵杆的Y轴上。
这是我的代码,操纵杆在X轴上正确倾斜,但Y轴的旋转不起作用:
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);
}我不知道为什么不工作,即使我这样做是正确的(也就是说,也许有一个更好的方法来实现它)。
非常感谢您的投入。
发布于 2020-12-08 22:25:24
我建议旋转它的数量,由星等决定,围绕一个单一的轴确定的方向。这将避免操纵杆旋转,在不对称操纵杆(如飞行员操纵杆)的情况下尤为明显:

评论中的解释:
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 stickInput应该围绕棍子的左右轴旋转旋钮。

发布于 2020-12-09 16:46:42
找出问题,atan2返回辐射角,但是代码假设它是欧拉度,当我完成转换时,它工作得很好。如果有人感兴趣,我将代码放在这里(而不是atan2函数中的更改):
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);
}https://stackoverflow.com/questions/65206320
复制相似问题