C# 使用addtorque将对象旋转到特定点
我想做一个太空游戏,但我不知道如何将我的宇宙飞船旋转到C# 使用addtorque将对象旋转到特定点,c#,unity3d,C#,Unity3d,我想做一个太空游戏,但我不知道如何将我的宇宙飞船旋转到vector3中的一个特定点,而addtorque 例如,为了消除速度,我当前计算轨迹的脚本如下 using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class CalculateTrajectory : MonoBehaviour { Vector3 Direction; //
vector3
中的一个特定点,而addtorque
例如,为了消除速度,我当前计算轨迹的脚本如下
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class CalculateTrajectory : MonoBehaviour
{
Vector3 Direction;
// Start is called before the first frame update
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Rigidbody rb = GetComponent<Rigidbody>();
Vector3 trajectory = rb.velocity; // Velocity of gameObject (Vector3)
Vector3 NextPosition = transform.position + trajectory;
Vector3 Direction = (NextPosition - transform.position) * 1000;
Debug.DrawRay(transform.position, Direction, Color.green, 0.2f);
}
}
使用System.Collections.Generic;
使用UnityEngine;
[RequiredComponent(类型)(刚体))]
公共类CalculateTrajory:单行为
{
矢量3方向;
//在第一帧更新之前调用Start
void Start()
{
刚体rb=GetComponent();
}
//每帧调用一次更新
void FixedUpdate()
{
刚体rb=GetComponent();
Vector3轨迹=rb.velocity;//游戏对象的速度(Vector3)
Vector3下一个位置=变换位置+轨迹;
矢量3方向=(下一个位置-变换位置)*1000;
Debug.DrawRay(transform.position,Direction,Color.green,0.2f);
}
}
(很抱歉我英语不好,我是一名来自德国的14岁学生)你需要找到旋转轴才能从当前航向(例如,
变换.向前
到所需的航向(rb.velocity
,如果我正确理解你的目标)。这可以使用叉积找到:
// Note: will be scaled by the sine of the angle between the two vectors
var axis = Vector3.Cross (transform.forward, rb.velocity.normalized);
然后,可以通过所需扭矩对其进行缩放,然后通过AddTorque
将所需扭矩施加到刚体上,以提供比例反馈。也就是说,这就像弹簧一样。但是,当角度>90度时,这将变弱(见下文)
您可能希望将刚体的角速度考虑在内,以包含一些阻尼。这可以通过减去刚体的角速度矢量(按某些因素缩放)来实现
请注意,为扭矩因子
和阻尼因子
找到合适的值需要进行一些调整,这取决于刚体的惯性矩(质量和形状的组合)
角度>90度的问题:
当角度在90度和180度之间时,当角度接近180度时,扭矩将再次下降。这可以通过计算半个角度的正弦来处理,这可以直接从矢量中完成:
// returns the sin(half angle between a and b).
// Loses the sign (thus direction) of the angle.
float HalfSin(Vector3 a, Vector3 b)
{
a = a.normalized;
b = b.normalized;
// (a - b).magnitude gives 2*sin(angle/2)
// using + will give 2*cos(angle/2)
return (a - b).magnitude * 0.5f;
}
然后,这可用于缩放两个角度的叉积的归一化结果,以获得半个角度的方向和大小,并且在0度时为0,在180度时为1(或者,如果没有对平行向量叉积产生的0向量进行归一化的问题,则为1)
使用上述想法,这是我对unity的FromToRotation的替代品(因为标准化问题导致它在非常小的角度下分离):
它在接近0度时表现良好,但在接近180度时会分离,但这是意料之中的:旋转轴未定义。未定义的旋转轴在0度时不是问题,但在180度时是一个非常大的问题(试着将一本书绕X、Y和Z轴旋转180度,或者在两者之间旋转:结果截然不同)
torque -= rb.angularVelocity * dampingFactor;
// returns the sin(half angle between a and b).
// Loses the sign (thus direction) of the angle.
float HalfSin(Vector3 a, Vector3 b)
{
a = a.normalized;
b = b.normalized;
// (a - b).magnitude gives 2*sin(angle/2)
// using + will give 2*cos(angle/2)
return (a - b).magnitude * 0.5f;
}
Quaternion fromtorot(Vector3 a, Vector3 b)
{
float ma = a.magnitude;
float mb = b.magnitude;
Vector3 mb_a = mb * a;
Vector3 ma_b = ma * b;
float den = 2 * ma * mb;
float mba_mab = (mb_a + ma_b).magnitude;
float c = mba_mab / den; // cosine of half angle
// find the rotation axis scaled by the sine of the half angle (s)
// using |a x b| = sin(angle) |a| |b| = 2 c s |a| |b|
// where c and s are the cosine and sine of the half hangle
// and mba_mab is 2 c |a| |b|
// c is not 0 until 180 degrees (vectors are anti-parallel)
Vector3 v = Vector3.Cross (a, b) / mba_mab;
return new Quaternion(v.x, v.y, v.z, c);
}