- Home /
Targeting the GameObject I collide with?
Hey guys, Im trying to create a combat system but the game must have no mouse. So I need the game to target the enemy when the player collides with its sphere collider. (Trigger).
function OnTriggerEnter()
{
collider.isTrigger = true;
targetWithinRange = true;
}
function OnTriggerExit () {
collider.isTrigger = true;
targetWithinRange = false;
}
What can I do to allow this function? + My enemy is a clone called from a handler threat called EnviromentHandler.
Answer by syclamoth · Dec 01, 2011 at 10:49 AM
OnTriggerEnter requires a collider paramater to be used with the physics engine-
function OnTriggerEnter(other : Collider)
{
// now you have access to the object which entered the trigger-
Debug.Log(other.gameObject.name);
}
The same with OnTriggerExit.
Thx it works great now all I need to do is set my variable
var target = GameObject
I need the Enemy(Clone) to be the new target. How would I do that?
Ok, a quick warning-
var target = GameObject;
will create a 'Type' variable. I'm honestly not sure if you can even do that.
You should use
var target : GameObject;
As for your question- inside the trigger functions
target = other.gameObject;
I'm not sure why you couldn't work that out from what I'd already posted.