- Home /
ignore child's collider
The MagicWand gameObject is the child of my Player gameObject. I increased the volume of the sphere collider of magicWand so it over-reaches the spherical collider of the Player. (It looks kinda like if PlayerGO was the nucleus of an atom and the collider of MagicWandGO was it's electron layer.)
The problem is, when I approach a PickUp item, it is picked up by the MagicWand's collider instead of Player's collider. Can this be fixed somehow?
This script is attached to MagicWand:
public class MagicWand_P1_scr : MonoBehaviour {
private bool _spellReady;
public int _spellCharged;
private bool _enemyWithinRange;
public GameObject _spell;
private Light _player2_light;
// enum Spells{Shock, Ignite, AcidSpray};
void Start ()
{
_spellReady = false;
_enemyWithinRange = false;
_player2_light = GameObject.FindWithTag ("Player_2").GetComponent<Light>();
}
// Update is called once per frame
void Update ()
{
Debug.Log("_enemyWithinRange " + _enemyWithinRange);
}
void OnTriggerEnter(Collider other)
{
if(other.name == "Player_2")
{
_player2_light.enabled = true;
_enemyWithinRange = true;
}
}
void OnTriggerExit(Collider other)
{
if(other.name == "Player_2")
{
_player2_light.enabled = false;
_enemyWithinRange = false;
}
}
}
This script is attached to Player:
public class PlayerMover_P1_scr : MonoBehaviour {
public float _movementSpeed;
private int _health;
MagicWand_P1_scr _magicWandController;
// Use this for initialization
void Start () {
GameObject _magicWandObject = GameObject.FindWithTag ("magicWand_1");
if (_magicWandObject != null)
{
Debug.Log ("magic wand found");
}
_magicWandController = _magicWandObject.GetComponent<MagicWand_P1_scr>();
if(_magicWandController != null)
{
Debug.Log ("magicWandController found");
}
_movementSpeed = 7f;
_health = 100;
}
void Update ()
{
if(Input.GetButtonDown ("Fire_P1"))
{
_magicWandController.CastSpell ();
}
}
void FixedUpdate()
{
float _moveHorizontal = Input.GetAxis ("Horizontal_P1");
float _moveVertical = Input.GetAxis ("Vertical_P1");
Vector3 _movement = new Vector3(_moveHorizontal, 0.0f, _moveVertical);
rigidbody.AddForce (_movement * _movementSpeed, ForceMode.Force);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.name == "PickUp")
{
Debug.Log ("P1_col = " + other.gameObject.tag);
Destroy (other.gameObject);
if(other.gameObject.tag == "IGNITE_pu")
{
xa._magicWand_1_SCRIPT.ChargeSpell("Ignite");
}
if(other.gameObject.tag == "SHOCK_pu")
{
xa._magicWand_1_SCRIPT.ChargeSpell("Shock");
}
if(other.gameObject.tag == "ACID_pu")
{
xa._magicWand_1_SCRIPT.ChargeSpell("Acid");
}
}
}
}
Answer by CodeElemental · Mar 10, 2014 at 10:11 AM
What you could try, is assign the magicwand collider to specific layer (different from the collider of the player), and regulate the collision in Player Preferences -> Physics (the checkboxes table).
For example :
Player in layer "Characters"
magicwand in "EquippedWeapons"
the pickup item "Items"
Uncheck the EquippedWeapons/Items checkbox.
I didn't even know something as "layers" existed in Unity. I'll give it a try. Is there any coding answer to this problem?
Well , you could disable the magic wand collider on start( if your game design allows it)
GameObject _magicWandObject = GameObject.FindWithTag ("magicWand_1");
_magicWandObject.collider.enabled = false;
Or attach a collision-check script on the magic wand collider so it ignores collision with another ONE collider explicitly (which i assume is not what you want).
Your answer
Follow this Question
Related Questions
How do you detect which child collider was hit? 6 Answers
Collider isn't working ? 2 Answers
Detect collision point?? 1 Answer
Trying to detect multiple points on a single collider 1 Answer