|
Hi everyone I'm rather new to Javascript and I've been experimenting with it for the past 3 days and read a lot of tutorials and forum posts - thanks in advance for those. What I can't figure out is how to have an object look at the nearest other object with a certain tag. I know I need an array for it and I've found some code for similar functions, but they either don't seem to work or do something I don't need to. Could anyone maybe assist me in how to write this? What I need is to have the object that code is attached to find the closest object with a specific tag (say "waypoint") and then look at it. Any help is much appreciated, thank you.
(comments are locked)
|
|
Something along these lines should be what you're after: Thank you. Unfortunately, this doesn't seem to work when I copy it into an empty Javascript. The script then gives me a load of errors (mainly semicolons missing and unity.Engine errors). An idea why that might be? Thank you very much for your help so far.
May 06 '10 at 06:05 PM
user-2441 (google)
Code is now tested and fixed! (had a few accidental pieces of c# syntax in there before!)
Jun 03 '10 at 09:31 AM
duck ♦♦
Awesome! this works for me... i've been atempting the same, you're a genius!!! :D
Feb 03 '11 at 08:39 PM
oinkoinkflapflap
awesome code thanks, cool if i use it ?
Apr 19 '11 at 02:03 AM
Chris 35
of course, that's why it's here :)
Apr 20 '11 at 07:58 PM
duck ♦♦
(comments are locked)
|
|
I was guided to the answer I was looking for, thank you very much! Here is the working code: How would i make a statement like if(waypoint > lastwaypoint)... ??? I must know if every waypoint has passed... Thanks
Aug 16 '10 at 05:03 PM
Henrique
tweaked it a bit ... and attached it with a collider to the water prefab.working great.thanks.now i can easily respawn my car from the last waypoint
Nov 06 '12 at 09:19 AM
parjanyaroy
(comments are locked)
|
|
hey man I noticed that Closest distance never resets and if it does it messes up my targeting here is what I have man see what you think it doesnt use LOOK AT because my code you can slow down the turret so if you wanted a huge battleship cannon you can do that with my code.
var attackRange = 30.0; var shootAngleDistance = 10.0; var closest: GameObject; function Update ()
{
} This sort of works but has a few issues. 1. It does not seem to do clean up for destroyed objects. MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
Any ideas on how to fix these issues?
Dec 12 '11 at 02:03 AM
Juce
(comments are locked)
|
|
just add one target transform and ignoring the Y axis and so this script can attached any object and can specify the transform function GetNearestTaggedObject(searchTag : String,otherTrans: Transform) : Transform { // and finally the actual process for finding the nearest object:
(comments are locked)
|
