- Home /
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 :)
Answer by GesterX · May 07, 2011 at 07:42 PM
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
}
}
Answer by Shaun 1 · May 07, 2011 at 07:47 PM
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.
Okay well then just modify my answer to suit your needs... Also don't post comments as answers!
Answer by Tommy · May 07, 2011 at 07:48 PM
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.
Answer by Matheusfig · Nov 18, 2012 at 03:02 PM
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);
}
}
}