How to get back to the first position animatedly in unity?

98 views Asked by At

I have a camera that I control it with the (W,A,S,D) keys...

What I want to do is that when press left mouse click ("Fire1") the camera turns back to the first position animatedly.

Is it possible to do it with Mecanim and create a dynamic animation file to do it?!

That's my code:

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);
    }

My camera position and rotation at the start is (0,0,0) but when I control my camera these parameters change so I want my camera to turns back to the first position (0,0,0) animatedly when I press left mouse button...

Something like:

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

There are 2 answers

0
Isma On BEST ANSWER

Instead of the animation, you could smooth out the camera movement:

Add the following variables to your script, the first one is used to control the amount of smoothness you want:

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

And then:

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

From your code, I can see that you are only changing rotation of the Camera. Following is my solution to it.
It saves the starting rotation at the start and later lerps to the start rotation when "Fire1" is pressed.
However, position is not handled here, as there's no position change in your code. But the concept is same. You can change the position in a similar way.

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;
        }
    }
}