我正在Unity3D中开发太阳系动画。行星围绕太阳旋转。但是我有一个模拟像Moon这样的卫星的问题。月亮应该正常环绕世界,月亮应该围绕世界旋转。由于世界在太阳周围转动,我在计算月球的真实旋转方面遇到了麻烦。我不想使用rotateAround(),因为它已被弃用。我需要使用Rotate()完成此操作。
这是我的planetScript
public class planetScript : MonoBehaviour {
public GameObject target;//target is Sun for World, World for Moon etc
public float rotateRatioCenter;//1 degree for World
private float rotateSpeedTarget;
public float rotateRatioAround;//365 for World
private float rotateSpeedAround;
public float counter = 0;
void Start () {
}
// Update is called once per frame
void Update () {
rotateSpeedTarget = rotateRatioCenter * gameMasterScript.rotateAroundCenterRatio;// rotateRatioCenter * 1 ,
rotateSpeedAround = rotateSpeedTarget * rotateRatioAround;
float yRotate = transform.eulerAngles.y;
transform.Rotate(Vector3.up, rotateSpeedAround);
rotateAroundTarget();
}
void rotateAroundTarget()//this is the method should be optimized
{
Quaternion quaRot = Quaternion.Euler(0, rotateSpeedTarget, 0);
transform.position = quaRot * (transform.position - target.transform.position) + target.transform.position;
}
}
答案 0 :(得分:0)
月亮正在路上!! 我在行星脚本中使用卫星对象解决了它,而不是为它们分配行星脚本。 PlanetScript现在有一个卫星GameObject。
旋转行星后,卫星围绕太阳旋转现在可以围绕行星旋转卫星(例如,围绕太阳旋转1度的世界围绕太阳旋转卫星1度并围绕太阳旋转月亮x度行星) 当世界围绕太阳旋转1度时,月球在世界范围内旋转大约12度(1月亮年= 30天差不多)。 新脚本
public class planetScript:MonoBehaviour {
public GameObject target;//target is Sun for World, World for Moon etc
public GameObject satellite;
public float rotateRatioCenter;//1 degree for World
public float rotateRatioAround;//365 for World
private float rotateSpeedTarget;
private float rotateSpeedAround;
public float satelliterotateRatioCenter;//12x for Moon
public float satelliteRotateRatioAround;//1 for Moon
private float satelliteRotateSpeedTarget;
private float satelliteRotateSpeedAround;
public float counter = 0;
void Start () {
}
// Update is called once per frame
void Update () {
rotateSpeedTarget = rotateRatioCenter * gameMasterScript.rotateAroundCenterRatio;// rotateRatioCenter * 1 ,
rotateSpeedAround = rotateSpeedTarget * rotateRatioAround;
satelliteRotateSpeedTarget = satelliterotateRatioCenter * gameMasterScript.rotateAroundCenterRatio;
satelliteRotateSpeedAround = satelliteRotateSpeedTarget * satelliteRotateRatioAround;
transform.Rotate(Vector3.up, rotateSpeedAround);
rotateAroundTarget();
}
void rotateAroundTarget()//this is the method should be optimized
{
Quaternion quaRot = Quaternion.Euler(0, rotateSpeedTarget, 0);
transform.position = quaRot * (transform.position - target.transform.position) + target.transform.position;
if (satellite != null)
{
satellite.transform.Rotate(Vector3.up, satelliteRotateSpeedAround);
satellite.transform.position = quaRot * (satellite.transform.position - target.transform.position) + target.transform.position;
Quaternion quaRotSat = Quaternion.Euler(0, satelliteRotateSpeedTarget, 0);
satellite.transform.position = quaRotSat * (satellite.transform.position - transform.position) + transform.position;
}
}
}
由于每架飞机都没有卫星,因此应更改脚本。