現状
操作キャラにキャラクターコントローラーとボックスコライダをつけている
敵キャラにはボックスコライダとリジッドボディをつけている
コンソールにはデバッグログがでているがアクティブにならない
試したこと
トリガーだといけるという記事を見てトリガーにしたが敵が地面から落ちているのかでてこなくなった
実現したいこと
敵に当たると敵についてる非アクティブのスモークをアクティブにしたい
該当コード
unity
1操作キャラコード 2public class PlayerCotroller : MonoBehaviour 3{ 4 private Vector3 touchStartPos;//タッチされた位置 5 private Vector3 touchEndPos;//離した位置 6 const int MinLane = -2;//最小レーン 7 const int MaxLane = 2;//最大レーン 8 const float LaneWidth = 1.0f;//レーン幅 9 public float gravity = 10.0f;//重力 10 public GameObject smoke;//煙 11 CharacterController controller; 12 13 Vector3 moveDirection = Vector3.zero; 14 int targetLane; 15 16 public float speedX = 4.0f;//横移動スピード 17 18 // Start is called before the first frame update 19 void Start() 20 { 21 controller = GetComponent<CharacterController>();//キャラコントローラー取得 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 //フリック入力(スマホ用) 28 Flick(); 29 //矢印入力(PC用) 30 if (Input.GetKeyDown("right")) MoveToRight(); 31 if (Input.GetKeyDown("left")) MoveToLeft(); 32 33 //X方向は目的のポジションまでの差分の割合で速度計算 34 float ratioX = (targetLane * LaneWidth - transform.position.x) / LaneWidth; 35 moveDirection.x = ratioX * speedX; 36 37 moveDirection.y -= gravity * Time.deltaTime;//重力加える 38 39 //移動実行 40 Vector3 gloabalDirection = transform.TransformDirection(moveDirection); 41 controller.Move(gloabalDirection * Time.deltaTime); 42 } 43 44 void Flick() 45 { 46 if (Input.GetKeyDown(KeyCode.Mouse0))//タップされたとき 47 { 48 touchStartPos = new Vector3(Input.mousePosition.x,//タップ位置 49 Input.mousePosition.y, Input.mousePosition.z); 50 } 51 if (Input.GetKeyUp(KeyCode.Mouse0))//離されたとき 52 { 53 touchEndPos = new Vector3(Input.mousePosition.x, 54 Input.mousePosition.y, Input.mousePosition.z);//離した位置 55 GetDirection(); 56 } 57 } 58 59 void GetDirection() 60 { 61 float directionX = touchEndPos.x - touchStartPos.x;//x距離 62 float directionY = touchEndPos.y - touchStartPos.y;//y距離 63 string Direction; 64 65 if (Mathf.Abs(directionY) < Mathf.Abs(directionX)) 66 { 67 if (30 < directionX) 68 { 69 //右フリック 70 Direction = "right"; 71 } else if (-30 > directionX) 72 { 73 //左フリック 74 Direction = "left"; 75 } 76 else 77 { 78 //タッチ 79 Direction = "touch"; 80 } 81 82 switch (Direction) 83 { 84 case "right": 85 MoveToRight(); 86 break; 87 case "left": 88 MoveToLeft(); 89 break; 90 } 91 } 92 } 93 public void MoveToRight() 94 { 95 if (controller.isGrounded && targetLane < MaxLane) targetLane+=2;//右レーンに移動 96 } 97 public void MoveToLeft() 98 { 99 if (controller.isGrounded && targetLane > MinLane) targetLane-=2;//左レーンに移動 100 } 101 102 public void OnControllerColliderHit(ControllerColliderHit hit) 103 { 104 105 } 106}
unity
1敵コード 2public class EnemyMove : MonoBehaviour 3{ 4 public float speed;//移動スピード 5 private int number; 6 public GameObject smoke; 7 // Start is called before the first frame update 8 void Start() 9 { 10 number = Random.Range(3, 10); 11 speed = number; 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 //フレームごとにzポジションを移動させる 18 transform.Translate(0, 0, -1.0f * speed * Time.deltaTime); 19 20 if (transform.position.z < -5.0f)//z座標が-5以下だった場合 21 { 22 23 Destroy(gameObject);//破壊 24 } 25 } 26 27 public void OnCollisionEnter(Collision collision) 28 { 29 if (collision.gameObject.CompareTag("Player")) 30 { 31 smoke.SetActive(true); 32 } 33 } 34}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/01/10 01:08
2020/01/10 01:17 編集
退会済みユーザー
2020/01/10 01:58
退会済みユーザー
2020/01/10 09:02 編集
2020/01/10 08:55
退会済みユーザー
2020/01/10 09:03
退会済みユーザー
2020/01/10 09:04
2020/01/10 09:06
退会済みユーザー
2020/01/10 09:19