• 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 /
This question was closed Nov 12, 2013 at 09:05 PM by fafase for the following reason:

Question is off-topic or not relevant

avatar image
Question by CB-TV · Nov 12, 2013 at 07:52 PM · movementairandomenemy

2D Random AI

I want an AI for an enemy to move through the X and Y axis in random directions. I want a certain location on the X and Y axis which they cannot go so they don't move there. Do you know how to do this?

Comment

People who like this

0 Show 3
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 softrare · Nov 12, 2013 at 07:54 PM 1
Share

Have you tried something already? Random numbers can be generated by Random.Range(). Why don't you post what you think should work?

avatar image CB-TV · Nov 12, 2013 at 08:14 PM 0
Share

I have a script like this: but I don't know how to make it go in other directions, then stop at a certain location.

     void Update () {
         
         print (walkingDirection);
  
         walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
  
         if (walkingDirection > 0.0f && transform.position.x >= wallRight)
             walkingDirection = -1.0f;
         else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
             walkingDirection = 1.0f;
  
         transform.Translate(walkAmount);
     }
 }
avatar image fafase · Nov 12, 2013 at 09:05 PM 0
Share

There you go :http://unitygems.com/basic-ai-character/

Your guy will go left, right, ahead, after you and others.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by TpkQ91 · Nov 12, 2013 at 09:03 PM

I put together a little simple example for you. I assumed boundaries but if you tell me more what exactly the condition is where your AI shouldn't move anymore I could help your further. Hope it helps :)

 //wall Boundaries
     float wall_left   = 5.0f;
     float wall_right  = Screen.width  - 5.0f;
     float wall_top    = Screen.height - 5.0f;
     float wall_bottom = 5.0f;
             
     //AI properties
     Vector2 AI_Position;
     float Speed = 0.3f;    
     
     //Get two Random values within a Range (Screen dimensions)
     float randomX = Random.Range(0,Screen.width);
     float randomY = Random.Range(0,Screen.height);
     
     //Create a Vector2 out of the Random values
     Vector2 randomXY = new Vector2(randomX,randomY);
     
     //Get the Direction from AI to the RandomXY generated
     Vector2 Direction = randomXY - AI_Position;
     
     //Normalize the Direction to apply appropriatly
     Direction = Direction.normalized;
     
     //Check that your AI is within your boundaries
     if(    AI_Position.x > wall_left && AI_Position.x < wall_right
         && AI_Position.y > wall_bottom && AI_Position.y < wall_top )
     {
     //Make AI move in the Direction (adjust speed to your needs)
         AI_Position += Direction * Speed;
     }
     else
     {
     //make your AI do something when its not within the boundaries
     //maybe generate a new direction?
     }
 
Comment
CB-TV
Hardik

People who like this

2 Show 4 · 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 CB-TV · Nov 13, 2013 at 04:36 PM 0
Share

10x "error CS0116: A namespace can only contain types and namespace declarations" 1x "Assets/Enemy AI.cs(24,15): error CS8025: Parsing error"

I'm not used to C#. What are the errors?

avatar image TpkQ91 · Nov 15, 2013 at 10:49 AM 0
Share

Where are you putting the code? Try Declaring the variables inside the class and set the variables inside a function of that class.

avatar image CB-TV · Nov 16, 2013 at 02:32 PM 0
Share

Yeah, I did that. But I don't really know C#. Could you possible show me what to do because I've put everything in a class but I don't know what else to do?

avatar image Jayrastaman · Oct 31, 2014 at 11:08 PM 0
Share

Were you ever shown what to do? I would like to get the script posted above to work too....If anyone can help, please post. Thank you.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Merging my enemy AI and target script together smart? 1 Answer

How do I animate my enemy randomly with time? 1 Answer

AI repeats movement and isnt randomised 0 Answers

Enemy AI Movement Decision Making 1 Answer

Unity AI/Enemy won't move towards target 0 Answers


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