|
Hey, I've been trying to make a TD game and one big problem I've run into is getting the tower to notice the enemies, which is easier? RayCast or OnTriggerEnter? heres the script I've been working on. but it doesn't work. its applied to a trigger sphere which has the towers top part as its child. var myEnemy; function OnTriggerEnter (enter : Collider) { if(enter.gameObject.tag == "Enemy") { myEnemy = enter.transform; } } function Update() { transform.LookAt(myEnemy.position); }
(comments are locked)
|
|
Well that should work... Except I'd specify the variable at the top.. Just because it's a good habit to have. I would probably also have a boolean so the tower doesn't look unless a target is acquired.
Also worthy of note - both objects need colliders for this to work, and at least one has to be set to trigger :) An additional note, a moving object won't reliably trigger OnBlah collision events unless it has a rigidbody - so add a Rigidbody to your enemies. You can set it to isKinematic to continue controlling them through script, too.
Oct 20 '10 at 06:25 AM
Loius
(comments are locked)
|
|
OnTriggerEnter is easier to use and has much better performance.
(comments are locked)
|
|
Could you ompare the transform.positions and use distance? If you can, this is very cheap and effective.
(comments are locked)
|
