动画相机以转动播放器,然后继续跟踪播放器

时间:2016-02-17 20:50:24

标签: c# unity3d unity5

为了解释我想要实现的目标,我正在使用" 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;
    }
}

1 个答案:

答案 0 :(得分:1)

请尝试使用Transorm.RotateAround()方法,而不是使用四元数进行设置:

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

条件是在超过原始旋转90度时停止。同样,向后工作以将其移回。

我希望有所帮助!