x


Distance thing with gameobject ?

hello guys, i want my AI's not to be too closer , so i made a code like this in the AI:

function Update()
{

var friend = GameObject.FindWithTag("UnGoro");

WHAT SHOULD I PUT HERE TO MAKE MY AI TO PUT A DISTANCE AT LEAST 10 BETWEEN HIS FRIEND?
}

please help :)

more ▼

asked May 07 '11 at 07:34 PM

Shaun 1 gravatar image

Shaun 1
15 6 7 11

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

4 answers: sort voted first

Distance is used to get the distance between two objects - not change them. Your error is coming from the fact that you need to do

friend.transform.position

Your script (this should be attached to your AI) should do something like this:

function Update()
{
if (Vector3.Distance(transform.position,friend.transform.position) > 10)
{
//some code to move the ai towards your player
}
else
{
//stop the ai
}
}
more ▼

answered May 07 '11 at 07:42 PM

GesterX gravatar image

GesterX
2.1k 13 16 37

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

actually the thing i want to make is different , i edited it , i don't want to make if statement or make the AI to move toward my player.

I'm something like observer in the scene, i have 10 characters in group A 10 characters in group B fighting with each other with swords.

I want group A characters to have at least 10 meters between each other.

more ▼

answered May 07 '11 at 07:47 PM

Shaun 1 gravatar image

Shaun 1
15 6 7 11

Okay well then just modify my answer to suit your needs... Also don't post comments as answers!

May 07 '11 at 07:53 PM GesterX
(comments are locked)
10|3000 characters needed characters left

I belive that "friend" already is a default var by Unity, try changing you var name.

Also, you'll need to do like:

var friend = GameObject.FindWithTag("UnGoro");

function Update()
{

if (Vector3.Distance(transform.position,friend.position) > 10)
{
 //Move the object 10, maybe using instanitate or something, the ofcourse can't SEE it walk
} 

Maybe you can attach waypoints to the players, that are as far as tou like away from the player, and the other players follow those waypoints.

more ▼

answered May 07 '11 at 07:48 PM

Tommy gravatar image

Tommy
156 20 22 27

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

Put This Script In Your Friend: Put the tag Player in your player and don't put your player in the "var Player : Transform".

var Player : Transform;
var MoveSpeed = 10.0;
private var StopSpeed = 0.0;
var RangeToStop = 10.0;
var damp = 5;

function Update () {
Player = GameObject.FindWithTag("Player").transform;

if(Vector3.Distance(Player.position,transform.position) > RangeToStop){
   var rotate = Quaternion.LookRotation(Player.position - transform.position);
   transform.rotation = Quaternion.Slerp(transform.rotation, rotate,damp * Time.deltaTime);
   transform.Translate(0,0, MoveSpeed * Time.deltaTime);
  }else{
  if(Vector3.Distance(Player.position,transform.position) < RangeToStop){
   var rotate2 = Quaternion.LookRotation(Player.position - transform.position);
   transform.rotation = Quaternion.Slerp(transform.rotation, rotate2,damp * Time.deltaTime);
   transform.Translate(0,0, StopSpeed * Time.deltaTime);
  }
  }
}
more ▼

answered Nov 18 '12 at 03:02 PM

Matheusfig gravatar image

Matheusfig
0 2

(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:

x2177
x984
x373
x337
x33

asked: May 07 '11 at 07:34 PM

Seen: 1126 times

Last Updated: Nov 18 '12 at 03:02 PM