摄像机震动效果

作者:追风剑情 发布于:2019-5-5 20:12 分类:Unity3d

示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraShake : MonoBehaviour
{
    private Transform ThisTransform = null;

    public float ShakeTime = 2.0f;
    public float ShakeAmount = 3.0f;
    public float ShakeSpeed = 2.0f;

    void Start()
    {
        ThisTransform = transform;
        StartCoroutine(Shake());
    }

    public IEnumerator Shake()
    {
        Vector3 origPosition = ThisTransform.localPosition;
        float ElapsedTime = 0.0f;

        while (ElapsedTime < ShakeTime)
        {
            Vector3 RandomPoint = origPosition + Random.insideUnitSphere * ShakeAmount;
            ThisTransform.localPosition = Vector3.Lerp(ThisTransform.localPosition, RandomPoint,
                Time.deltaTime * ShakeSpeed);
            yield return null;
            ElapsedTime += Time.deltaTime;
        }

        ThisTransform.localPosition = origPosition;
    }
}

运行测试

2.gif

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号