- Home /
AI random target
Hello everyone , i have code like this :
var Hedef : Transform; // hedef means the Target var Gorusmesafe = 1000; // Gorusmesafe means the distance which the AI can see var sword:GameObject; var runningspeed : float = 0.08;
function Update () { if (Hedef && Follow()) { var HedefRotation = Quaternion.LookRotation(Hedef.position - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, HedefRotation , Time.deltaTime * 2.0); } var targets : GameObject[] = GameObject.FindGameObjectsWithTag("ZulDurak"); var targetNumber : int = Random.Range(0,targets.length - 1); Hedef = targets[targetNumber].transform;
}
function Follow() {
if(Vector3.Distance(transform.position, Hedef.position) > Gorusmesafe )
{
print("too far");
return false;
}
if (Vector3.Distance(transform.position,Hedef.position ) < Gorusmesafe )
{
transform.Translate(Vector3.forward * runningspeed);
}
if (Vector3.Distance(transform.position,Hedef.position) < 5 )
{
Attack();
}
if (Vector3.Distance(transform.position,Hedef.position ) < 1 )
{
transform.Translate(Vector3.forward * -1 * Time.deltaTime);
}
if (Vector3.Distance(sword.transform.position,Hedef.position ) < 1 )
{
Hedef.transform.Translate(Vector3.forward * -4 * Time.deltaTime);
}
var hit : RaycastHit;
if(Physics.Linecast(transform.position,Hedef.position,hit))
{
if (carpma.collider.gameObject.tag !="Character")
{
}
else
{
}
}
return true;
}
function Attack()
{
sword.animation.Play("Tam Vurma");
}
This is the AI code from war game which is with sword shield etc.
But when i launch the game :
- Problem : In 1 vs 1 it's cool , ally AI finds the enemy AI and enemy AI finds ally AI then attack each other and when they collide and their GorusMesafe is less then 1 like written in the code they force each other. But when 3 or more AI's come together , i think because of that " transform.Translate(Vector3.forward -1 Time.deltaTime); " thing they start flying in the air like this :
http://i1104.hizliresim.com/2011/4/22/11632.png
How can i solve this ?
2.Problem : When i first start the game , ally and enemy AI's select their targets randomly for example A character selects C character. But when i close the game and open again , the A character selects AGAIN the C character , they alwasy select the same character after 1 random selection , how can i solve these problems , please someone help and explain clearly :)
Greetings , please help :)
Answer by Stib · Apr 23, 2011 at 06:25 PM
I have a game with lots of enemies pushing about as well. Putting this code into the update function of the bad guys seemed to fix the jumping problem.
function Update() { //Move about first
//Zero upward movement
transform.position.y = 0;
}
Next what you need to do is "seed" the random number generator. You see random number generators are not truly "random". The sequence of number they produce is based on a seed. I'm getting different random numbers each time I start up my projects (so maybe someone else could tell us why you are getting the same numbers each startup) but you could still try this
function Start()
{
Random.seed = Time.time;
}
I just realized your AIs are choosing targets every frame. This means that about 60 times a second your AIs are changing targets. This could cause them to gather into a group like you see. Ins$$anonymous$$d have the AI choose a target and not change target until it is dead (you could add other conditions for more complicated behavior).
Answer by Inan · Apr 23, 2011 at 08:49 PM
first , thank you for answer.
I tried that position.y but when i do this they are not flying around this is good but they all come together and fps reduces to 4 or 5 , i also don't want them to come together like in the photo , i thought i can prevent this by making their selection random , so i put the code you gave me right into the script but it didn't work :/
Your answer
Follow this Question
Related Questions
Object selection 1 Answer
Random LookAt or Random Target 2 Answers
how to refer to enemy health 2 Answers