I am trying to get my tower placment script to detect when the player is near enogh to allow them to place a tower in the specified areas. not sure how to get it to work however. please help.
Hey,
What you could do, is use Vector3.Distance to check how far an object is to another...
Try this code:
var player : Transform;
var distance = 50;
private var playerCloseEnough = false;
function Update(){
if(Vector3.Distance(transform.position, player.position) < distance){
playerCloseEnough = true;
}
else{
playerCloseEnough = false;
}
}
Attach that to the tower. Then drag the player object into the variable slot in the inspector... That will detect when the player comes within 50 units (or whatever the measurement is, I forgot) of the tower, and will then set the `playerCloseEnough` variable to true.... (You can also change the value of 50 to whatever you want!!!!!) It will then also set the value to false when the player gets too far away!!!!
I think that you do need colliders on both of these objects though, and I'm not sure, but you might need a rididbody....
You could also use raycasting, but that would only detect in one certain direction....
Hope this helps, and if you need more help, feel free to comment back!!!!!!!!!!!!!!!!!!
-Grady
May be a little hard on performance, but raycasting would work.
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html