やりたいこと
Unityでオブジェクトを飛ばし、一定時間たつまでオブジェクトを撃てないようにし撃った後一定時間たったら戻ってくるようにしたい。
試したこと
今回のソースで、デバックログでif文内に入っていることは確認できましたが、オブジェクトが移動されず、待機モーションと同じ位置のまま、留まっています。
Unity
1using UnityEngine; 2using System.Collections; 3 4public class EnemyBullet : MonoBehaviour 5{ 6 //GameObject Bulletold2; 7 // Use this for initialization 8 public GameObject Enemy; 9 public GameObject note; 10 public GameObject note1; 11 12 public Quaternion noteRot; 13 public Quaternion note1Rot; 14 15 public bool Flagnote = true; 16 public bool Flagnote1 = true; 17 18 Rigidbody rb; 19 20 public float EnemyBulletradius = 10.0f; 21 public float EnemyBulletpower = 100.0f; 22 public int EnemyTimeA = 0; 23 public int EnemyTimeB = 0; 24 public int EnemyTimeC = 0; 25 26 private Vector3[] OldnotePos = new Vector3[4]; 27 private Quaternion[] OldnotefRot = new Quaternion[4]; 28 29 void Start() 30 { 31 OldnotefRot[0] = note.transform.localRotation; 32 OldnotefRot[1] = note1.transform.localRotation; 33 } 34 35 // Update is called once per frame 36 void Update() 37 { 38 //noteObjPosという名前でnoteのトランスホームのローカルポジションを変更 39 var noteObjPos = note.transform.localPosition; 40 //noteObjRotという名前でnoteのトランスホームのローカルロケーションを変更 41 var noteObjRot = note.transform.localRotation; 42 43 //note1ObjPosという名前でnote1のトランスホームのローカルポジションを変更 44 var note1ObjPos = note1.transform.localPosition; 45 //note1ObjRotという名前でnote1のトランスホームのローカルロケーションを変更 46 var note1ObjRot = note1.transform.localRotation; 47 if (Input.GetKeyDown(KeyCode.M)) 48 { 49 Debug.Log("始まり"); 50 // if (EnemyTimeC >= 60) 51 Flagnote = false; 52 } 53 if (Flagnote == false) 54 { 55 Debug.Log("始まったよ"); 56 //noteのRotの座標を変更 57 noteObjRot.x = 180.0f; 58 noteObjRot.y = 0.0f; 59 noteObjRot.z = 200.0f; 60 //noteのRotの座標を反映 61 note.transform.localRotation = noteObjRot; 62 ////noteのPos.zをFlagnoteがfalseの時だけ0.5f-していく 63 noteObjPos.z = noteObjPos.z - 0.5f; 64 //noteのPosの座標を反映 65 note.transform.localPosition = noteObjPos; 66 Flagnote1 = false; 67 EnemyTimeB += 5; 68 69 if (EnemyTimeB > 15) 70 { 71 if (Flagnote1 == false) 72 { 73 Debug.Log("Flagnote1on"); 74 //note1のRotの座標を変更 75 note1ObjRot.x = 180.0f; 76 note1ObjRot.y = 0.0f; 77 note1ObjRot.z = 200.0f; 78 //note1のRotの座標を反映 79 note1.transform.localRotation = note1ObjRot; 80 ////note1のPos.zをFlagnote1がfalseの時だけ0.5f-していく 81 note1ObjPos.z = note1ObjPos.z -= 0.5f; 82 //note1のPosの座標を反映 83 note1.transform.localPosition = note1ObjPos; 84 } 85 } 86 87 if (EnemyTimeA > 120) 88 { 89 //noteのロケーションの変更 90 noteObjRot.x = -180.0f; 91 noteObjRot.y = 0.0f; 92 noteObjRot.z = 60.0f; 93 //noteのポスティングの変更 94 noteObjPos.z = 1.2f; 95 96 //note1のロケーションの変更 97 note1ObjRot.x = -180.0f; 98 note1ObjRot.y = 100.0f; 99 note1ObjRot.z = 280.0f; 100 //note1のポスティングの変更 101 note1ObjPos.z = 1.2f; 102 103 noteRot = noteObjRot; 104 note1Rot = note1ObjRot; 105 106 //noteのロケーションの反映 107 note.transform.localRotation = OldnotefRot[0]; 108 //SwordAのポスィングの反映 109 note.transform.localPosition = noteObjPos; 110 111 //note1のロケーションの反映 112 note1.transform.localRotation = OldnotefRot[1]; 113 //note1のポスィングの反映 114 note1.transform.localPosition = note1ObjPos; 115 116 Debug.Log("帰還"); 117 Flagnote = true; 118 Flagnote1 = true; 119 120 EnemyTimeA = 0; 121 EnemyTimeB = 0; 122 EnemyTimeC = 0; 123 } 124 } 125 EnemyTimeA += 5; 126 EnemyTimeC += 15; 127 } 128 void OnCollisionEnter(Collision col) 129 { 130 if (col.gameObject.tag == "Player") 131 { 132 col.gameObject.GetComponent<PlayerController>().Damage(1); 133 //Destroy(gameObject); 134 Debug.Log("(>_<)"); 135 136 // 力を発生させる場所 137 Vector3 explosionPos = transform.position; 138 139 // 中心点から設定した半径の中にあるcolliderの配列を取得する。 140 Collider[] colliders = Physics.OverlapSphere(explosionPos, EnemyBulletradius); 141 142 foreach (Collider hit in colliders) 143 { 144 // 力を及ぼしたいオブジェクトにRigidbodyが付いていることが必要(ポイント) 145 Rigidbody rb = hit.GetComponent<Rigidbody>(); 146 147 if (rb != null) 148 { 149 // 取得したRigidbodyに力を加える 150 // 3つの引数(加える力の強さ、力の中心点、力を及ぼす半径) 151 rb.AddExplosionForce(EnemyBulletpower, explosionPos, 5.0f); 152 } 153 } 154 } 155 } 156}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/22 01:19
2018/08/22 01:56
2018/08/22 02:08
2018/08/23 01:27