『unity main camera を揺らす』

unity 2020.1.14f

ゲームスタート前の演出として画面を揺らしています。

惑星がプレイヤーに衝突した事が分かるように画面を揺らす演出に使用しています。

<スクリプト>

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

public class CameraShake : MonoBehaviour
{
public void Shake(float duration, float magnitude)
{
StartCoroutine(DoShake(duration, magnitude));
}

private IEnumerator DoShake(float duration, float magnitude)

var pos = transform.localPosition;

var elapsed = 0f;

while (elapsed < duration)

{

var x = pos.x + Random.Range(-2f, 2f) * magnitude;

var y = pos.x + Random.Range(-2f, 2f) * magnitude;

transform.localPosition = new Vector3(x, y, pos.z);

elapsed += Time.deltaTime;

yield return null;

}

transform.localPosition = pos;

}

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメントする

CAPTCHA


目次
閉じる