简体   繁体   English

Unity根据其他对象角度旋转对象

[英]Unity rotate object depending on other objects angle

I have two Gameobjects. 我有两个Gameobjects。 The basic Question: "When I spin circle 1, I want to spin circle 2 in the same way manipulated by factor x" 基本问题:“当我旋转圆圈1时,我想以因子x操纵的相同方式旋转圆圈2”

How do I sync the rotation around each of their local axis of circle 2 with the interactable rotation of circle one and scale that rotation by factor x? 如何围绕圆2的每个局部轴周围的旋转与圆1的可相互作用的旋转同步并将该旋转按因子x进行缩放? Setting the transform.right equal doesnt work, there are still to many degrees of freedom. 设置transform.right相等不起作用,仍有很多自由度。 (Local Axis, because I want one or both gameobjects to be also tilted, but unrelated to one another.) (Local Axis,因为我希望一个或两个游戏对象也倾斜,但彼此无关。)

Trying math with rotational matrices didnt really work out based on the fact of them being evaluated every frame and thereby spinning Object 2 for eternity. 使用旋转矩阵尝试数学并没有真正解决,因为它们每帧都被评估,从而使对象2永久旋转。

Thanks so much! 非常感谢!

在此输入图像描述

Assuming you rotate only around one single axis (as shown in the images) you can get the rotation difference in degrees of circle2 using eg Quaternion.Angle every frame 假设您只围绕一个单轴旋转(如图所示),您可以使用例如Quaternion.Angle每帧以圆度2获得旋转差异

private Quaternion lastCircle2Rot;

//...

float rotDelta = Quaternion.Angle(lastCircle2Rot, circle2.transform.rotation);
lastCircle2Rot = circle2.transform.rotation;

than turn the circle1 accordignly using eg Transform.RotateAround 而不是使用例如Transform.RotateAround转动circle1

public float multiplier;

// e.g. rotate around local x = right
Vector3 YourAxis = circle1.transform.right;
circle1.transform.RotateAround(Vector3.zero, YourAxis, rotDelta * multiplier);

if using eg the right or another "clean" vector you could also simply use 如果使用例如正确或另一个“干净”的矢量,你也可以简单地使用

circle1.transform.Rotate(Vector3.right, rotDelta * multiplier);

as it is done in local space by default. 因为它默认在本地空间完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM