2016-02-17 142 views
0

因此,为了解释我想要实现的目标,我使用“E”键将照相机绕播放器旋转90度,然后继续跟踪播放器。我不知道该如何顺利完成 - 到目前为止,我已经尝试了使摄像机跟踪播放器的动画,并且一旦动画完成,动画就会返回到原始位置。动画照相机转动播放器,然后继续跟踪播放器

这里是我当前的代码:

using UnityEngine; 
using System.Collections; 

public class TCam : MonoBehaviour { 


    public Transform target; 
    private Animator animator; 
    private Vector3 positionOffset ; 

    int i =0; 
    // Use this for initialization 
    void Start() { 
     //positionOffset = target.transform.position + transform.position; 
     positionOffset = new Vector3(-10, 10,0); 
     animator = GetComponent<Animator>(); 
     animator.enabled = true; 
    } 

    // Update is called once per frame 
    void Update() { 
     if (Input.GetKeyDown (KeyCode.E)) { 
      animator.SetTrigger ("switch"); 

      transform.rotation = Quaternion.Euler (45, transform.rotation.eulerAngles.y + 90, 0); 
      target.transform.rotation = Quaternion.Euler (45, target.transform.rotation.eulerAngles.y + 90, 0); 

      if (i == 0) { 
       positionOffset = new Vector3 (0, 10, 10); 
       i++; 
      } else if (i == 1) { 
       positionOffset = new Vector3 (10, 10, 0); 
       i++; 
      } else if (i == 2) { 
       positionOffset = new Vector3 (0, 10, -10); 
       i++; 
      } else if (i == 3) { 
       positionOffset = new Vector3 (-10, 10, 0); 
       i = 0; 
      } 
     } else if (Input.GetKeyDown (KeyCode.Q)) { 
      transform.Rotate (-45, 0, 0); 
      transform.Rotate (0, -90, 0); 
      transform.Rotate (45, 0, 0); 

      target.transform.Rotate (-45, 0, 0); 
      target.transform.Rotate (0, -90, 0); 
      target.transform.Rotate (45, 0, 0); 
     } 
     transform.position = target.position + positionOffset; 
    } 
} 
+0

只是为了澄清:你的核心问题是当按下'E'时,顺利地在相机周围旋转相机;和动画是一个单独的事情。我是否正确阅读? – andeart

+0

@andeart对不起,让我感到困惑,我的意思是我尝试从动画窗口动画相机,但是当我做了我不能让它跟踪播放器,或者如果我可以动画会搞砸,当它被称为从代码,但是是的,我想一次在相机周围顺畅地旋转相机90度。 – AlexNotTheLion

+0

您是否尝试过使用http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html? – andeart

回答

1

而是与四元数设置的,请尝试使用Transorm.RotateAround()方法如下:

transform.RotateAround(target.transform.position, Vector3.up, 40 * Time.deltaTime); 

与条件,过去它的原始停止时,它的90度回转。同样,向后移动以将其移回。

我希望有帮助!

+0

没错。为了善良,**从不使用四元数**。 – Fattie