找回密码
 注册帐号

扫一扫,访问微社区

hnl6868618

unity相机抖动代码分享

2020-6-28 12:18 |个人分类:Unity

using UnityEngine;
using System.Collections;
namespace ElementGirl.shiweijie
{
    public class CameraShake : MonoSingleton<CameraShake>
    {
        // 抖动目标的transform(若未添加引用,怎默认为当前物体的transform)
        public Transform camTransform;
        [Tooltip("持续抖动的时长")]
        public float shake = 0f;
        // 抖动幅度(振幅)
        //振幅越大抖动越厉害
        public float shakeAmount = 0.7f;
        public float decreaseFactor = 1.0f;
        [Tooltip("是否抖动")]
        public bool isShake = false;
        private float oldShake;
        Vector3 originalPos;
        void Awake()
        {
            oldShake = shake;
            if (camTransform == null)
            {
                camTransform = GetComponent(typeof(Transform)) as Transform;
            }
        }
        void OnEnable()
        {
            originalPos = camTransform.localPosition;
        }
 
        void Update()
        {
            if (isShake == true)
            {
                if (shake > 0)
                {
                    camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
                    shake -= Time.deltaTime * decreaseFactor;
                }
                else
                {
                    shake = 0f;
                    camTransform.localPosition = originalPos;
                    isShake = false;
                }
            }
            if (isShake == false)
            {
                shake = oldShake;
            }
        }
    }
}

全部作者的其他最新博客

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

Unity大学【第二期】!