asteroidsteam icon

UnityCamSmoothMove

asteroidsteam | PRO | 02/02/19 08:51:56 PM UTC | 0 ⭐ | 374 👁️ | Never ⏰ | []
C# |

2.02 KB

|

None

|

0 👍

/

0 👎

using UnityEngine;
 
public class CamMove : MonoBehaviour
{
    public Transform target;
    public Transform start;
 
    public float smoothMoveSpeed = 0.03f;
    public float smoothRotateSpeed = 0.125f;
    public Vector3 targetOffset;
    public Vector3 startOffset;
    private bool goto_target;
    private bool goback;
 
    public bool autoFaceTarget = true;
 
    private int state;
 
    public void goToTarget()
    {
        goto_target = true;
        goback = false;
    }
 
    public void goBack()
    {
        goto_target = false;
        goback = true;
    }
 
    public int getState()
    {
        return state;
    }
 
    public void toggleSmoothRotate()
    {
        if (autoFaceTarget)
        {
            autoFaceTarget = false;
        }
        else
        {
            autoFaceTarget = true;
        }
    }
 
    void FixedUpdate()
    {
        if (goto_target && goback == false)
        {
            state = 0;
            Vector3 desiredPosition = target.position + targetOffset;
            Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothMoveSpeed);
            transform.position = smoothedPosition;
            if (autoFaceTarget)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), smoothRotateSpeed);
            }
        }
        else if (goback && goto_target == false)
        {
            state = 1;
            Vector3 desiredPosition = start.position +startOffset;
            Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothMoveSpeed);
            transform.position = smoothedPosition;
            if (autoFaceTarget)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(start.position - transform.position), smoothRotateSpeed);
            }
        }
        else
        {
            state = 2;
        }
    }
}

Comments