Camera 控制移动的,针对万向锁做的变化

控制相机移动的脚本

using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine.Events;

public class Camera_Singleton
{
    private static Camera_Singleton instance;

    public static Camera_Singleton Instance
    {
        get
        {
            if (instance == null)
                instance = new Camera_Singleton();

            return instance;
        }
    }

    public Sequence mySequence;//继承自Tween,密封,用于记录动画

    [Header("摄像机是否在移动")]
    public bool isMoving = false;   //摄像机是否正在移动

    [Header("摄像机是否正在晃动")]
    public bool isShake = false;   //摄像机是否正在摇晃

    [Header("单次摇晃时间")]
    public float shakeDuration;

    [Header("摇晃强度")]
    public float shakeStrength;

    /// <summary>
    /// 摄像机移动
    /// </summary>
    /// <param name="position"></param>
    /// <param name="wow"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="distance"></param>
    /// <param name="t"></param>
    /// <param name="method"></param>
    public void WowCameraMove(Vector3 position, WowMainCamera wow, float x, float y, float distance, float t, UnityAction method)
    {
        isMoving = true;
        mySequence.Kill();
        Tweener t1 = wow.target.transform.DOMove(position, t).SetEase(Ease.Linear);
        wow.x %= 360;
        wow.y %= 360;
        float deviation = Mathf.Abs(wow.x - x);
        if (deviation > 180)
        {
            if (x > 0)
            {
                if (wow.x > 180)
                {
                    x += 360;
                }
                else
                {
                    x -= 360;
                }
            }
            else
            {
                if (wow.x < -180)
                {
                    wow.x += 360;
                }
                else
                {
                    x += 360;
                }
            }
        }
        Tweener t2 = DOTween.To(() => wow.x, a => wow.x = a, x, t).SetEase(Ease.Linear);
        Tweener t4 = DOTween.To(() => wow.y, b => wow.y = b, y, t).SetEase(Ease.Linear);
        Tweener t3 = DOTween.To(() => wow.desiredDistance, c => wow.desiredDistance = c, distance, t).SetEase(Ease.Linear);
        mySequence = DOTween.Sequence();
        mySequence.Append(t1)
            .Insert(0, t2)
            .Insert(0, t3)
            .Insert(0, t4)
            .AppendCallback(() => { isMoving = false; method(); });
    }

    //摄像机移动,旋转与位移持续时间分离
    public void WowCameraMove_Splite(Vector3 position, WowMainCamera wow, float x, float y, float distance, float t_move, float t_rotate, Ease ease, UnityAction method)
    {
        isMoving = true;
        mySequence.Kill();
        Tweener t1 = wow.target.transform.DOMove(position, t_move).SetEase(ease);
        wow.x %= 360;
        wow.y %= 360;
        float deviation = Mathf.Abs(wow.x - x);
        if (deviation > 180)
        {
            if (x > 0)
            {
                if (wow.x > 180)
                {
                    x += 360;
                }
                else
                {
                    x -= 360;
                }
            }
            else
            {
                if (wow.x < -180)
                {
                    wow.x += 360;
                }
                else
                {
                    x += 360;
                }
            }
        }
        Tweener t2 = DOTween.To(() => wow.x, a => wow.x = a, x, t_rotate).SetEase(ease);
        Tweener t4 = DOTween.To(() => wow.y, b => wow.y = b, y, t_rotate).SetEase(ease);
        Tweener t3 = DOTween.To(() => wow.desiredDistance, c => wow.desiredDistance = c, distance, t_move).SetEase(ease);
        mySequence = DOTween.Sequence();
        mySequence.Append(t1)
            .Insert(0, t2)
            .Insert(0, t3)
            .Insert(0, t4)
            .AppendCallback(() => { isMoving = false; method(); });
    }

    public void SetCameraPos(WowMainCamera wow, Vector3 position, float x, float y)
    {
        wow.target.position = position;

        wow.x = x;

        wow.y = y;
    }

    public void WowCameraMove(Vector3 position, WowMainCamera wow, float x, float y, float distance, float t, Ease ease, UnityAction method)
    {
        isMoving = true;
        mySequence.Kill();
        Tweener t1 = wow.target.transform.DOMove(position, t).SetEase(ease);
        wow.x %= 360;
        wow.y %= 360;
        float deviation = Mathf.Abs(wow.x - x);
        if (deviation > 180)
        {
            if (x > 0)
            {
                if (wow.x > 180)
                {
                    x += 360;
                }
                else
                {
                    x -= 360;
                }
            }
            else
            {
                if (wow.x < -180)
                {
                    wow.x += 360;
                }
                else
                {
                    x += 360;
                }
            }
        }
        Tweener t2 = DOTween.To(() => wow.x, a => wow.x = a, x, t).SetEase(ease);
        Tweener t4 = DOTween.To(() => wow.y, b => wow.y = b, y, t).SetEase(ease);
        Tweener t3 = DOTween.To(() => wow.desiredDistance, c => wow.desiredDistance = c, distance, t).SetEase(ease);
        mySequence = DOTween.Sequence();
        mySequence.Append(t1)
            .Insert(0, t2)
            .Insert(0, t3)
            .Insert(0, t4)
            .AppendCallback(() => { isMoving = false; method(); });
    }


    /// <summary>
    /// 摄像机摇晃
    /// </summary>
    /// <param name="wow"></param>
    /// <param name="duration">用时(*4)</param>
    /// <param name="strength">力度</param>
    public void CameraShake(WowMainCamera wow, float duration, float strength) //一直运行为摄像机震动效果,运行一帧为摄像机轻微摆动效果
    {
        if (isShake)
            return;

        shakeDuration = duration;
        shakeStrength = strength;
        isShake = true;
        float tmpX = wow.x;
        Tweener t1 = DOTween.To(() => wow.x, r => wow.x = r, wow.x - strength, duration).SetEase(Ease.Linear);
        Tween t2 = DOTween.To(() => wow.x, r => wow.x = r, tmpX, duration).SetEase(Ease.Linear);
        Tweener t3 = DOTween.To(() => wow.x, r => wow.x = r, tmpX + strength, duration).SetEase(Ease.Linear);
        Tweener t4 = DOTween.To(() => wow.x, r => wow.x = r, tmpX, duration).SetEase(Ease.Linear);
        mySequence = DOTween.Sequence();
        mySequence.Append(t1).Append(t2).Append(t3).Append(t4).SetLoops(-1, LoopType.Restart);
    }


    public void KillCameraMove(WowMainCamera wow)
    {
        if (mySequence != null && mySequence.IsPlaying())
            mySequence.Kill();
        isMoving = false;
        isShake = false;
    }

    public void KillCameraMove()
    {
        if (mySequence.IsPlaying())
            mySequence.Kill();
    }
}

  

posted on 2018-07-18 07:23  虾球  阅读(132)  评论(0)    收藏  举报

导航