Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Inan · Apr 23, 2011 at 11:15 AM · airandomtargetselect

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 :

  1. 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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stib · May 03, 2011 at 06:26 PM 0
Share

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).

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Object selection 1 Answer

Random LookAt or Random Target 2 Answers

how to refer to enemy health 2 Answers

AI spawning areas. What are the ways to make them? 1 Answer

How to turn a spawned enemy ( AI ) target on player?/ Why are my Hierarchy object turn prefab then no longer hold the transform object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges