画像な位置にコリジョンがありそのオブジェクトがプレイヤーの子のオブジェクトだった場合の子のオブジェクトのCollisionの取得方法です。
説明
子のスクリプトで親のスクリプト取得してそこに関数の引数にCollision,collider を入れて親オブジェクトの関数が参照されるという仕組みです。
子のオブジェクト
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)
{
//ここに処理を書く
}
}