Unityでオブジェクトにぶつかったらカメラを動かすという処理をしようとしたのですが、それが思い通りに動いてくれません。
こちらがソースです。
using
1using System.Collections.Generic; 2using UnityEngine; 3 4public class ShakeCamera : MonoBehaviour { 5 public void Shake(float duration, float magnitude) 6 { 7 StartCoroutine(DoShake(duration, magnitude)); 8 } 9 10 private IEnumerator DoShake(float duration, float magnitude) 11 { 12 var pos = transform.localPosition; 13 14 var elapsed = 0f; 15 16 while (elapsed < duration) 17 { 18 var x = pos.x + Random.Range(-1f, 1f) * magnitude; 19 var y = pos.y + Random.Range(-1f, 1f) * magnitude; 20 21 transform.localPosition = new Vector3(x, y, pos.z); 22 23 elapsed += Time.deltaTime; 24 25 yield return null; 26 } 27 28 transform.localPosition = pos; 29 } 30} 31
そしてこちらが実際に揺らすコードです。
public ShakeCamera shake; public void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("GameOver")) { GetComponent<Rigidbody>().velocity = Vector3.zero; transform.position = new Vector3(0, 0, -34); LifeManager(); click = 0; } if (other.gameObject.CompareTag("Enemy")) { shake.Shake(5f, 5f); }
エラーは出ていません。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/30 04:25