x


How can I make my gameObject find the nearest object with a specific tag and rotate to look at it?

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.

more ▼

asked May 05 '10 at 08:44 AM

user-2441 (google) gravatar image

user-2441 (google)
81 1 1 5

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Something along these lines should be what you're after:
(Edit : now tested & working!)

// the tag to search for (set this value in the inspector)
var searchTag = "Respawn";

// the frequency with which to re-scane for new nearest target in seconds 
// (set in inspector)
var scanFrequency = 1.0;

// the current target
private var target : Transform;


function Start() {
    // set up repeating scan for new targets:
    InvokeRepeating("ScanForTarget", 0, scanFrequency );
}

function Update() {
    // we rotate to look at the target every frame (if there is one)
    if (target != null) {
        transform.LookAt(target);
    }
}

function ScanForTarget() {
    // this should be called less often, because it could be an expensive
    // process if there are lots of objects to check against
    target = GetNearestTaggedObject();

}

function GetNearestTaggedObject() : Transform {
    // and finally the actual process for finding the nearest object:

    var nearestDistanceSqr = Mathf.Infinity;
    var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag); 
    var nearestObj : Transform = null;

    // loop through each tagged object, remembering nearest one found
    for (var obj : GameObject in taggedGameObjects) {

        var objectPos = obj.transform.position;
        var distanceSqr = (objectPos - transform.position).sqrMagnitude;

        if (distanceSqr < nearestDistanceSqr) {
            nearestObj = obj.transform;
            nearestDistanceSqr = distanceSqr;
        }
    }

    return nearestObj;
}
more ▼

answered May 05 '10 at 09:26 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

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)
10|3000 characters needed characters left

I was guided to the answer I was looking for, thank you very much! Here is the working code:

function Update () {
    var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoint"); 
    var closest: GameObject; 
    var closestDist = Mathf.Infinity; 

    for (waypoint in waypoints) { 
        var dist = (transform.position - waypoint.transform.position).sqrMagnitude; 

        if (dist < closestDist) { 
            closestDist = dist; 
            closest = waypoint; 
            } 
    } 
    transform.LookAt(closest.transform); 
}
more ▼

answered May 06 '10 at 10:42 PM

user-2441 (google) gravatar image

user-2441 (google)
81 1 1 5

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)
10|3000 characters needed characters left

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 closestDist = Mathf.Infinity; 

var attackRange = 30.0; var shootAngleDistance = 10.0;

var closest: GameObject;

function Update () {
var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoint");

    for(waypoint in waypoints)
    {
    var dist = (transform.position - waypoint.transform.position ).sqrMagnitude;   

    if(dist <= closestDist)
    {
            closestDist = dist;
            closest = waypoint;       
    }else
    {
        closestDist += 100;
    }
    //Debuging ClosestDist
    if(Input.GetKeyDown("t"))
    {
    print("ClosestDist: " + closestDist);
    }


        var targetPoint = closest.transform.position;
        var targetRotation = Quaternion.LookRotation ( targetPoint - transform.position , Vector3.up);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);

        // If we are almost rotated towards target - fire one clip of ammo
        var forward = transform.TransformDirection(Vector3.forward);
        var targetDir = closest.transform.position - transform.position;

        if (Vector3.Angle(forward, targetDir) < shootAngleDistance && closest.gameObject.tag == "waypoint")
            SendMessage("Fire");


    }

}

more ▼

answered Sep 12 '10 at 07:56 PM

BocoProgrammer gravatar image

BocoProgrammer
2 3

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.

  1. When new objects are spawned, the rotation gets confused and the rotation stops offest or between the "waypoints"

Any ideas on how to fix these issues?

Dec 12 '11 at 02:03 AM Juce
(comments are locked)
10|3000 characters needed characters left

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:

    var nearestDistanceSqr : float = Mathf.Infinity;
    var taggedGameObjects : GameObject[] = GameObject.FindGameObjectsWithTag(searchTag); 
    var nearestObj : Transform = null;

    // loop through each tagged object, remembering nearest one found
    for (var obj : GameObject in taggedGameObjects) {

        var objectPos = obj.transform.position;
        var distanceSqr = (objectPos - Vector3(otherTrans.position.x,objectPos.y,otherTrans.position.z)).sqrMagnitude;

        if (distanceSqr < nearestDistanceSqr) {
            nearestObj = obj.transform;
            nearestDistanceSqr = distanceSqr;
        }
    }

    return nearestObj;
}
more ▼

answered May 03 '12 at 07:07 AM

luozitian gravatar image

luozitian
51 1 4 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2071
x720
x285
x204
x18

asked: May 05 '10 at 08:44 AM

Seen: 12723 times

Last Updated: Mar 14 at 05:20 PM