How can I get (In Javascript) a OnTriggerEnter function in a Update function for a Enemy AI. This is the code:
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag(“Player”).transform;
}
Impossible… Because OnTriggerEnter and Update is a different function. So, the function cannot be called in another function. If you want to do that you can make a variable to solve that For Example : if you want to call that Trigger was true. here it this
var yourVar = false;
function OnTriggerEnter(other : Collider)
{
yourVar = true;
}
function Update()
{
if(yourVar)//if yourVar is true
{
//do something in here
}
}
Sure you can move the rocket or whatever in Update and if that move resulted in a collision with a trigger, then OnTriggerEnter will be called and you explode it there.
You do not need to check if a collision happened in Update. You can of course do it i.e. with RigidBody SweepTest() SweepTestAll() but you do not have to.
Anyway for such stuff to work both game objects need to have a rigidbody and collider attatched and one has to be flagged with IsTrigger if you want to use OnTriggerEnter.