0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Unity] 子のオブジェクトのCollisionを取得する方法

Posted at

画像な位置にコリジョンがありそのオブジェクトがプレイヤーの子のオブジェクトだった場合の子のオブジェクトのCollisionの取得方法です。

説明

子のスクリプトで親のスクリプト取得してそこに関数の引数にCollision,collider を入れて親オブジェクトの関数が参照されるという仕組みです。

スクリーンショット 2021-07-12 102520.png
スクリーンショット 2021-07-12 122921.png

子のオブジェクト

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackCol : MonoBehaviour
{

    GameObject moveControl;
    MoveControl moveControlScript;

    // Use this for initialization
    void Start()
    {       
        moveControl = transform.parent.gameObject;
        moveControlScript = moveControl.GetComponent<MoveControl>();
    }

    void Update()
    {
        
    }

    //親のmoveControlスクリプトで判定
    private void OnTriggerStay(Collider other)
    {
        //Debug.Log("Attack col");
        moveControlScript.AttackStay(other);
    }

    //親のmoveControlスクリプトで判定
    private void OnTriggerExit(Collider other)
    {
        //Debug.Log("Attack col");
        moveControlScript.AttackExit(other);
    }
}

親オブジェクト

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveControl : MonoBehaviour
{
    private bool isAttack;

    void Start()
    {     
        isAttack = false;    
    }

    void Update()
    {
      
    }

    //子のTriggerStay
    public void AttackStay(Collider other)
    {
      //ここに処理を書く
    }

    //子のTriggerExit
    public void AttackExit(Collider other)
    {
      //ここに処理を書く
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?