【问题标题】:How to get back to the first position animatedly in unity?如何统一动画回到第一位置?
【发布时间】:2018-02-13 12:10:35
【问题描述】:

我有一个相机,我用 (W,A,S,D) 键控制它...

我想要做的是,当按下鼠标左键(“Fire1”)时,相机会动画地回到第一个位置。

有没有可能用 Mecanim 做到这一点,并创建一个动态动画文件来做到这一点?!

这是我的代码:

void Update () 
{
    if (Input.GetKey(KeyCode.W)) 
    {
        Cam.transform.Rotate (0, 0, 2);
    }

    if (Input.GetKey(KeyCode.S) )
    {
        Cam.transform.Rotate (0, 0, -2);
    }

    if (Input.GetKey(KeyCode.D)) 
    {
        Cam.transform.Rotate (0, 2, 0);
    }

    if (Input.GetKey(KeyCode.A)) 
    {
        Cam.transform.Rotate (0, -2, 0);
    }

一开始我的相机位置和旋转是 (0,0,0),但是当我控制我的相机时,这些参数会发生变化,所以我希望我的相机在我开始时动画地转回第一个位置 (0,0,0)按鼠标左键...

类似:

if (Input.GetButtonDown("Fire1")) 
{
    Cam.GetComponent<Animation> ().Play ();
}

【问题讨论】:

  • “第一位置”是什么意思?
  • 游戏开始时的摄像机位置
  • 你不妨贴一些代码;)例如,你如何移动你的相机?

标签: c# animation unity3d animator


【解决方案1】:

您可以平滑相机移动,而不是动画:

将以下变量添加到您的脚本中,第一个用于控制您想要的平滑度:

public float smoothTime = 0.2f;
private Vector3 velocity = Vector3.zero;

然后:

if (Input.GetButtonDown("Fire1")) {
    Vector3 targetPosition = new Vector3(0,0,0);
    Cam.transform.position = Vector3.SmoothDamp(Cam.transform.position, targetPosition, ref velocity, smoothTime);
}

【讨论】:

    【解决方案2】:

    从您的代码中,我可以看到您只是在更改相机的旋转。以下是我的解决方案。
    当按下“Fire1”时,它会在开始时保存开始旋转,稍后会保存到开始旋转。
    但是,此处不处理位置,因为您的代码中没有位置变化。但是概念是一样的。您可以通过类似的方式更改位置。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class CamTest : MonoBehaviour {
        public float animSpeed = 1.0f;
        public Camera Cam;
        private Quaternion startRotation;
        private bool doRotate = false;
    
        // Use this for initialization
        void Start () {
            //Cam = GetComponent<Camera> ();
            startRotation = transform.rotation;
        }
    
        void Update () {
    
            if (Input.GetKey(KeyCode.W)) 
            {
                Cam.transform.Rotate (0, 0, 2);
            }
            if (Input.GetKey(KeyCode.S) )
            {
                Cam.transform.Rotate (0, 0, -2);
            }
    
            if (Input.GetKey(KeyCode.D)) 
            {
                Cam.transform.Rotate (0, 2, 0);
            }
            if (Input.GetKey(KeyCode.A)) 
            {
                Cam.transform.Rotate (0, -2, 0);
            }
    
            if (Input.GetButtonDown("Fire1")) {
                Debug.Log ("Fire1");
                doRotate = true;
            }
            if(doRotate) DoRotation ();
        }
    
        void DoRotation(){
            if (Quaternion.Angle(Cam.transform.rotation, startRotation) > 1f) {
                Cam.transform.rotation = Quaternion.Lerp(Cam.transform.rotation, startRotation,animSpeed*Time.deltaTime);
            } else {
                Cam.transform.rotation = startRotation;
                doRotate = false;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多