Hi guys when my player hit my enemy or jumps on him i want the enemy to disappear, the enemy has been tagged but when i run my game it will not disappear can you see what is wrong, i have declared var collide to false, if anyone can see what i am doing wrong. Should i be using OnControllerCollider instead of OnTrigger as this is for events and not for collision detection:
the code is attached to my player
var collide = false;
function OnTriggerEnter(collide : Collider)
{
// Check for enemies via a tag
if(collide.gameObject.tag == "enemy")
{
return true;
}
}
Okay i have changed the code to the following and it have issued a print statement and its saying its collided but will not disappear, i ave tried defining colide as false and then return true when i hit the enemy but its not working a bit stuck - my updated code, also the enemy goes straight through the player, confused:
var collide = false;
function OnControllerColliderHit(collide : ControllerColliderHit)
{
// Check for enemies via a tag
if(collide.gameObject.tag == "enemy")
{
print ("you have hit the enemy");
return true;
}
}
Where is the code that tells your enemy to disappear?
Wait, I think I see what youâre doing.
That âcollideâ at the top is a boolean.
The âcollideâ that is a parameter in âOnControllerColliderHitâ is not the same as the bool collide up above it. Itâs a totally different thing.
So if youâre returning true, youâre not actually changing the âcollideâ boolean to true, youâre just returning âtrueâ to nothing.
After that print statement, do this:
this.collide=true
Iâm assuming that when âthis.collide==trueâ, then something will happen in another script.
Alternatively, name one of the variables something else so that you can tell them apart.
His question isnât very clear, he says âDissapearâ perhaps he just wants it to go invisible and not destroy itself, in order to do that you would basically remove the mesh from the object.
i thought the variable collide was the same as where ControllerColliderHit was, so if i was to change it to say hit and then true would this work along with destroying the gameobject please see below guys, or could i just .this.collide=true in place of destroying the object would that work:
var collide = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
// Check for enemies via a tag
if(hit.gameObject.tag == "enemy")
{
Destroy (gameObject);
return true;
}
}
function OnControllerColliderHit(hit : ControllerColliderHit)
{
//Print the value of the collider:
Debug.Log("Colliding gameobject tag name:"+ hit.gameObject.tag);
// Check for enemies via a tag
if(hit.gameObject.tag == "enemy")
{
//Destroy my gameobject instance... (won't use it anymore)
Destroy (gameObject.FindWithTag("enemy"));
}
}
Anyway, if what you meant for âdisappearâ is only to get off the render mesh of the gameobject, you should go this way:
function OnControllerColliderHit(hit : ControllerColliderHit)
{
//Print the value of the collider:
Debug.Log("Colliding gameobject tag name:"+ hit.gameObject.tag);
// Check for enemies via a tag
if(hit.gameObject.tag == "enemy")
{
//Please do not destroy my gameobject, as I will use it again one more time maybe... so here I'm just making its mesh invisible
gameObject.FindWithTag("enemy").renderer.enabled=false;
}
}
var hasCollidedWithEnemy : false;
function OnTriggerEnter(collide : Collider){
// Check for enemies via a tag
if(collide.gameObject.tag == "enemy") {
Destroy(collide.gameObject);
hasCollidedWithEnemy = true;
}
}
of you want to hide it, then as shown above.
var hasCollidedWithEnemy : false;
var ObjectRenderers;
function OnTriggerEnter(collide : Collider){
// Check for enemies via a tag
if(collide.gameObject.tag == "enemy") {
ObjectRenders = collide.gameObject.GetComponentsInChildren<Renderer>(false);
for (var i: 0; i < ObjectRenders.Length; i++){
Renderer renderer = ObjectRenders[i];
renderer.enabled = false;
}
hasCollidedWithEnemy = true;
}
}
The above code is psuedo so dont rape me if it doesnt work, and i hate unity script.
Thanks for all the feedback it all really helps me out here big time so this is the code I have written and has managed to make the enemy disappear what I need to know how I get and explosion when I have collided with my object:
private var killenemy = false;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "falloff") // fall of platform
{
dead = true; // he is dead
}
else if (hit.gameObject.tag == "enemy")
{
Destroy(hit.gameObject); // remove from the scene
killenemy = true;
}
}