問題点
今回、リンク内容のスクリプトを参考にして、画面の揺れを実装しようとしたが、画面が揺れない問題が発生したので揺れるようにしてあげたい。
実装したいこと
こちらを用いて、画面の揺れを実装したい。
Inspector
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class CameraShakeScript : MonoBehaviour { 6 7 public void Shake(float duration, float magnitude) 8 { 9 StartCoroutine(DoShake(duration, magnitude)); 10 } 11 12 private IEnumerator DoShake(float duration, float magnitude) 13 { 14 var pos = transform.localPosition; 15 16 var elapsed = 0f; 17 18 while (elapsed < duration) 19 { 20 var x = pos.x + Random.Range(-1f, 1f) * magnitude; 21 var y = pos.y + Random.Range(-1f, 1f) * magnitude; 22 23 transform.localPosition = new Vector3(x, y, pos.z); 24 25 elapsed += Time.deltaTime; 26 27 yield return null; 28 } 29 30 transform.localPosition = pos; 31 } 32} 33
C#
1Player 2public CameraShakeScript camerashake 3 private void Update() 4 { 5 if(Input.GetKeyDown(KeyCode.W)) 6 { 7 camerashake.Shake(0.5f, 1f); 8 Debug.Log("Shake"); 9 } 10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/06 00:54