C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MobScript : MonoBehaviour { 6 7 new Rigidbody2D rigidbody2D; 8 public int speed = -2; 9 bool OK = true; 10 11 void Start() 12 { 13 rigidbody2D = GetComponent<Rigidbody2D>(); 14 } 15 16 void Update() 17 { 18 rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); 19 } 20 21 private void OnTriggerEnter2D(Collider2D collision) 22 { 23 if(collision.gameObject.tag == "Wall") 24 { 25 if (OK == true) 26 { 27 speed = 2; 28 Vector2 scale = transform.localScale; 29 scale.x = -6.7f; 30 transform.localScale = scale; 31 OK = false; 32 } 33 34 if(OK == false) 35 { 36 speed = -2; 37 Vector2 scale = transform.localScale; 38 scale.x = 6.7f; 39 transform.localScale = scale; 40 OK = true; 41 } 42 } 43 } 44} 45
Wallというタグに衝突したときに、スプライトと進行方向を反転させるスクリプトを書きました。
ですが、OnTriggerEnter2Dが全く反応しません。
動くものにはRigidBody2DとBoxCollider2Dをつけています。
衝突される側にはBoxCollider2Dのみです。staticオブジェクトの子オブジェクトになっています。isTriggerにはチェックをつけています。
解決方法が分からないので、教えていただけるとありがたいです。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/03 05:17