C# 让游戏对象复制目标位置和旋转速度/角度速度

C# 让游戏对象复制目标位置和旋转速度/角度速度,c#,unity3d,rigid-bodies,C#,Unity3d,Rigid Bodies,我有一个游戏对象,我需要通过只更改速度和角度速度来跟随另一个游戏对象。 对于职位而言,这很容易: clone.velocity = (target.transform.position - clone.transform.position) * k; 但是对于angularvelocity我找不到,似乎我需要使用四元数 Vector3 oldpoint = clone.transform.TransformDirection(Vector3.up); Vector3 n

我有一个
游戏对象
,我需要通过只更改
速度
角度速度
来跟随另一个
游戏对象
。 对于职位而言,这很容易:

    clone.velocity = (target.transform.position - clone.transform.position) * k;
但是对于
angularvelocity
我找不到,似乎我需要使用
四元数

    Vector3 oldpoint = clone.transform.TransformDirection(Vector3.up);
    Vector3 newpoint = target.transform.TransformDirection(Vector3.up);
    var av=Quaternion.FromToRotation(oldpoint,newpoint);
但是它返回一个
四元数
角速度
是一个
矢量3
,我找不到如何转换它。

使用:


等等,您不希望
具有与位置相同的方向,而是希望平滑地朝着它移动,对吗?;)对于角速度,您可以简单地使用
av.eulerAngles*somefactor
@德胡戈:不,我想要相同的位置和旋转。我将尝试使用eulerAngles…如果您想要相同的位置和旋转,两个对象将重叠。在这种情况下,您可以只使用
MovePosition
MoveRotation
…顺便说一句,您还知道,除了
TransformDirection(Vector3.up)
之外,您可以只使用
transform.up
;)除了角度需要以弧度转换,并且当它返回正角度(0到360)时,当大于180时,必须减去360:
if(angle>180)angle-=360;角度*=Mathf.Deg2Rad
你能纠正一下吗?我接受你的答案。@entertoize我很惊讶
到角度轴
会给出大于180的角度。我编辑了我的答案,将
角度
转换为带符号的等效值。无论如何,不需要专门转换为弧度。相反,请使用较小的
angularVelocityFactor
值。
Vector3 oldpoint = clone.transform.TransformDirection(Vector3.up);
Vector3 newpoint = target.transform.TransformDirection(Vector3.up);
Quaternion av = Quaternion.FromToRotation(oldpoint,newpoint);

float angle;
Vector3 axis;

av.ToAngleAxis(out angle, out axis);

// Convert to signed angle
angle = Mathf.Repeat(angle + 180f, 360f) - 180f;

// depending on how rapidly you want it to rotate to match it 
// don't set too high or it might overshoot
float angularVelocityFactor = 0.01f; 

clone.angularVelocity = (angularVelocityFactor * angle) * axis;