Good day,
I have a simple javascript that return a message once triggered, but somehow I cannot get it to work.
I have a Gameobject named DoorKey,and when my player collides with it, it must show in the console that it collided.
I also tagged the Gameobject with the name DoorKey
function OnControllerColliderHit(hit: ControllerColliderHit)
{
if(hit.gameObject.tag == "DoorKey")
{
print("Key picked up");
}
}
Any Idea why? Must my Doorkey be a Prefab? for it to work?
Best Regards,
Jakes
It may have to do with your key prefab. Does it have a collider not in trigger mode?
I would do it like this:
function OnTriggerEnter( other : Collider){
if( other.gameObject.tag == "Player"){
print("Picked up the Key!");
}
}
Of course, for this to work, the character controller must be tagged as “Player”, and you must have a collider on the object, that is set as trigger.
If you only want it to do the print statement the first time the player walks into the object, simply add a boolean,
called “pickedUp”, or something like that, having it initially as false… In the if statement, check for both collision and the boolean. Then, in the if statements execution, chance the boolean to “true”.