- Home /
Best way to detect specific collisions in child objects
Hi all,
I am making my 1st game and right now, I have a simple game were two players need to jump on the other's head to kill him. It's a 2 player game. Pretty basic as you can see.
I am currently using hitboxes on the head child and feet children of the Player1 and Player2 gameobjects. I made triggers on both, places feets and heads in a different layer, and if there is a collision, it triggers a script. This script is triggered by both, so I check the highest one with transform.y positions to kill the right one.
The problem is that I want to have multiple characters in my fighting game, not only two, and right now, the trigger script cannot be on both gameobjects (playey1 and player2) as it would trigger twice (for everything, like score, animations, etc).
I would like to replace the triggers with OnCollisionEnter2D or something like that. Is there a way to check if the head has been hit without rigibodies (as feets and heads should not collide with the word and should not affect the characters) and without triggers? Then, if the head has been hit, to give the point to the right player and destroy the other gameobject?
I need something that could be in all characters scripts OR something that would be on none. Right now, I have a GameMaster empty gameobject with a couple of scripts on it. If I could check that specific collision and manage what happen to both players from it, it would be really nice.
I hope I'm not too lost for simple answers haha.
Thanks anyway guys and gals!
It triggers twice since both characters have a trigger, that is how triggers works sadly.
Script should only trigger for the object the script instance is on, unless you have used static variables, functions or classes.
The other culprit is using Find tag Player to perform any operations whatsoever.
I have a script on one player with only one method OnTriggerEnter2D, both both characters are going in. I tried many different variations of triggers with OnTriggerEnter2D and both gameobject always gets in, wathever I try. Inside the method, I tried using the hitInfo to destroy the other gameobject, with no success, they always get both destroyed. It makes no sense to me. That's why I would prefer something without triggers, as they trigger for every gameobject in the collision.
Answer by toddisarockstar · Feb 15, 2015 at 09:58 PM
you will need if statements to check some info about the other object. if something jumps on your head... this will destroy whichever object is lower if your position Y is up.
function OnCollisionEnter2D(otherguy: Collision2D) {
if(otherguy.gameObject.transform.position.y>transform.position.y)
{Destroy(gameObject);}
}
or maybe you can check the object name before you so something??????? this should work:
var timesiwaskicked=0;
function Update(){
if(timesiwaskicked>5){Destroy(gameObject);}
}
function OnCollisionEnter2D(otherguy: Collision2D) {
if(otherguy.gameObject.transform.name=="kungfufoot"){
if(transform.name=="myhead"){
timesiwaskicked=timesiwaskicked+1;}}
}
hopefully this gets you thinking in the right direction.
and for the other part of your question. if you want to send data to your master object.....i will assume the object "Game Master" has a script attached called "Brain", and there is a variable in it called "playerone".
var bigmaster:GameObject:
var brainscript:Component;
bigmaster=GameObject.Find("Game Master");
brainscript=bigmaster.GetComponent("Brain");
brainscript.playerone=brainscript.playerone+1;
this will change a variable in your game master object from any script, anywhere. it works with the webplayer but idk about flash.
Answer by MESSI007 · Feb 21, 2017 at 04:30 PM
i got a cool solution ////////////////////////////////// in parent :
public void OnCollisionEnter2D(Collision2D coll)
{
WhenColliding(coll);
}
// virtual to override it in child class
public virtual void WhenColliding(Collision2D coll)
{
print("ther parent is talking");
}
///////////////////////////////////////////////and in child class
override
public void WhenColliding(Collision2D coll)
{
print("child is talking");
}