Simple proximity detection help

hey guys

i need to make a script that i can apply to a player, so that when he gets close to an object, certain code is run… I don’t understand that much about raycasting, and i’ve seen people recommending to do it all different ways, like with spheres.

Can someone please help me out!!!

thanks

-Grady

1 Answer

1

Two options:

  • trigger objects - see OnTriggerEnter

  • just check the distance every frame:

     if (Vector3.Distance(transform.position, badplace.position) < 2.5)
         DoStuff();
    

how would you do that with javascript?

–

so where it says "badplace.position" would i change that to the object that i want to see if the player is near?

–

Yes. The script might have that as a variable: var badplace : Transform; you then drag the appropriate game object to that variable in the inspector.

–

this is my code: var objective : Transform; function Update () { if (Vector3.Distance(transform.position, objective.position) < 10){ Application.LoadLevel("game_scene_01"); } } the console has no errors to do with this script, but when i get right close to the object, nothing happens... is it something to do with using the Update() function or is it meant to be a different function? thanks

–

no its ok, i must have forgot to do something because its working now!!!!!!! Thanks a lot! -Grady

–