在3D物体周围飞行

时间:2014-07-18 03:26:18

标签: rotation unity3d quaternions

我正在制作游戏太空模拟器。我的太空舱必须以3D模式绕地球飞行。 我为spaceheep做了一个简单的控制器:

public GameObject objToMove=GameObject.Find("Player");
public GameObject zeroPoint=GameObject.Find("Earth");
public float farFromZero=10;

 void Update(){

    Vector2 Ldirection;
    Ldirection.x = Input.GetAxis ("Horizontal");
    Ldirection.y = Input.GetAxis ("Vertical");


    MoveObj (objToMove, Ldirection.x, Ldirection.y);

}

我也做了一个单独的课,因为我想从另一个班级(触摸屏)获得控制权

public void MoveObj(GameObject LobjToMove, float AxsHoriz, float AxsVert){

    AxsHoriz = AxsHoriz * speed;
    AxsVert = AxsVert * speed; 
    AxsHoriz *= Time.deltaTime;
    AxsVert *= Time.deltaTime;

    LobjToMove.transform.Translate(AxsHoriz, AxsVert, 0);
    //spase has to look at the earth
    LobjToMove.transform.LookAt (zeroPoint.transform.position);

    ZeroPointMagnitude (LobjToMove);
}

//fixing the distance between earth and spacesheep
void ZeroPointMagnitude(GameObject LobjToMove)
{
    Vector3 direction = zeroPoint.transform.position - LobjToMove.transform.position;
    float distance = direction.magnitude;
    if (distance > farFromZero||distance < farFromZero) {
        LobjToMove.transform.position += direction.normalized * (distance - farFromZero);
    }

}

我把相机放在太空中。
它看起来很好,并且空间飞行在地球周围,但看起来它绕着枢轴飞行,只是当它到达推力时使半径变小,如果我向上或向下按压它就不会四处走动 - 它停在底部和楼上/>
我试着让它飞得好3个月,我使用了Raycast或Quaternions,它不能正常工作。

看起来需要花费几个月的时间,我请你帮助我在物体周围飞行玩家而不停止使用pulus(或相机翻转)

1 个答案:

答案 0 :(得分:0)

所以我们走了

   Vector3 rotDir=new Vector3(AxsVert,AxsHoriz,0);
   float gypotenuse=Mathf.Sqrt(AxsHoriz*AxsHoriz+AxsVert*AxsVert);
   gypotenuse=(gypotenuse>1)?1:gypotenuse;

        objToMove.transform.RotateAround (zeroPoint.transform.position,
                                          objToMove.transform.TransformDirection(rotDir), 
                                          gypotenuse
                                          );