前提・実現したいこと
上記の方のブログでキャッシュしたほうがいいとのことですが、
Camera.main.transform.positionをStart関数内でキャッシュする場合、どのようなコードにするのがベストなのかが知りたいです。
該当のソースコード
現時点でのコードを添付します。
Update関数内で、Camera.main.transform.positionを設定していますが、
これは処理としては重いのかなと考えています。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5[RequireComponent(typeof(Rigidbody))] 6public class BigExplosionBurst : MonoBehaviour 7{ 8 private Vector3 _cv = new Vector3(0, 1f, -5f); 9 private Vector3 _v, _vy = Vector3.zero; 10 private GameObject _ex = null; 11 private Rigidbody _rb = null; 12 private bool _flag = true; 13 14 void Start() 15 { 16 //スタートでのキャッシュで、Camera.main.transform.positionの変数に格納し、高速化を図りたい 17 _rb = GetComponent<Rigidbody>(); 18 _ex = GameObject.FindGameObjectWithTag("BigExplosion"); 19 } 20 21 void Update() 22 { 23 //Camera 現在、Update関数内のため、処理が重いと思っています。 24 Vector3 sv = transform.position; 25 sv.y = 1f; 26 Camera.main.transform.position = sv + _cv; 27 //Input 28 float x = Input.GetAxis("Horizontal"); 29 float z = Input.GetAxis("Vertical"); 30 _v = new Vector3(x, 0, z); 31 //Jump 32 float JumpPower = Input.GetAxis("Fire1"); 33 if (JumpPower > 0) 34 { 35 if(_flag) 36 { 37 _vy = new Vector3(0, 500f, 0); 38 } 39 _flag = false; 40 } 41 else 42 { 43 _flag = true; 44 } 45 } 46 47 private void FixedUpdate() 48 { 49 _rb.AddForce(_v + _vy); 50 } 51 52 private void OnTriggerEnter(Collider collision) 53 { 54 if (collision.gameObject.CompareTag("BigExplosion")) 55 { 56 _ex.transform.position = collision.gameObject.transform.position; 57 _ex.GetComponent<ParticleSystem>().Play(); 58 Destroy(collision.gameObject); 59 } 60 } 61} 62
試したこと
なし
補足情報(FW/ツールのバージョンなど)
Unity 2020.1.6f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/18 08:09