newbie: check distance between two objects

Hi Guys,

First I want to apologize for posting newbie questions hehe. I am more of an artist rather than coder, and Ive scanned through these foruns, tried to adapt codes from here and all, with no success! I have been trying all afternoon to get this to work. Here is my code

var _target : GameObject;
var _player : GameObject;
var distanciaMinima : float;

function Update(){

var distance = Vector3.Distance (this.transform.position, _target.transform.position);
Debug.Log(distance);

	if (distance < distanciaMinima)
	{
		Debug.Log("Near");
	}
	else
	{
		Debug.Log("Far");
	}
	
}

It works as far as returning a value. But it does not update as I move my character. I realise that what is happening is perhaps when I assign “_target” as a GameObject, it is reffering to the object in the library and not of the one ingame? If so, how do I refer to the one in game??
[/code]

Try this code

var target : Transform;
var distanciaMinima : float;
public var distance : float;

function Start(){

target = GameObject.Find("GameObject Name").transform;       
}

function Update(){

distance = Vector3.Distance (transform.position, target.position);
Debug.Log(distance);

   if (distance < distanciaMinima)
   {
      Debug.Log("Near");
   }
   else
   {
      Debug.Log("Far");
   }
   
}

Just change GameObject Name to the name of your game object you want to target in the hierarchy.

Hi Deikkan,

It works! Thank you. Although it doesnt seem to update as I walk closer to the target. I have placed a code where if I press a key, it shows the distance variable value in the console, but the value is the same as it was in the start. How can I do so it is constantly updating it. So it will do action depending on the distance. Basically this is for interaction between a character and another. Once one is close to the other, action happens. Here is a screenie. The first number is where the guy was. The second number is where he is now. All 3 numbers are the same no matter his location.

[/img]

There’s a line in code:

distance = Vector3.Distance (transform.position, target.position);

The “transform.position” refers to the game object that the script is attached to. In other words, if the script is attached to your player, then it will return the distance from your player to the target.

(I’m tiptoe-ing around the question: is this script attached to your player?)

Yes it is. Oh god. It was attached to the gameobject and not to the model itself. Now that I added the script onto the actual model under the gameobject it works! Thank you so much!!!

Without seeing your whole project, I’m really not sure. But I have the feeling Deikkan or somebody will figure it out.

Is the script working how you want it? There may be problems if there is more than one game object with the same name as the one you want to target, in which case you could attach the script to the target game object and have it target the player.

Also, I made distance a public variable so you should be able to see it updating in the inspector of the object you attached the script to.

Hey Deikkan! Its working beautifully! if you want to see the video of it running look at the link below. Yea I will probably attach the script to the NPC guy since there will be several more in the scene.

Again, thank you very much for your help!

http://www.eivix.com/games/video/video5.html