使用Player Unity 3D旋转相机

时间:2018-12-22 17:00:55

标签: c# unity3d

我有一个向前移动,弹跳并旋转的球,我希望相机跟随它并随之旋转,以便相机始终从后面看球。所以我把脚本放到了下面,但是相机在旋转时没有看着球!

  

注意::我不希望将相机用作球的孩子,因为我不希望相机弹跳。

相机脚本:

public Transform Ball;
private Vector3 Offset;

// Use this for initialization
void Start () {

    Offset = transform.position - Ball.transform.position;

}

// Update is called once per frame
void LateUpdate () {

    transform.position = new Vector3(Ball.transform.position.x + Offset.x, transform.position.y, Ball.transform.position.z + Offset.z);
    transform.rotation = Ball.transform.rotation;
}

1 个答案:

答案 0 :(得分:0)

 [SerializeField]
 private Transform target;

 [SerializeField]
 private Vector3 offsetPosition;

 [SerializeField]
 private Space offsetPositionSpace = Space.Self;

 [SerializeField]
 private bool lookAt = true;

 private void Update()
 {
     Refresh();
 }

 public void Refresh()
 {
     if(target == null)
     {
         Debug.LogWarning("Missing target ref !", this);

         return;
     }

     // compute position
     if(offsetPositionSpace == Space.Self)
     {
         transform.position = target.TransformPoint(offsetPosition);
     }
     else
     {
         transform.position = target.position + offsetPosition;
     }

     // compute rotation
     if(lookAt)
     {
         transform.LookAt(target);
     }
     else
     {
         transform.rotation = target.rotation;
     }
 }

目标是玩家的游戏对象