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
0
Question by JoshMBeyer · Jan 26, 2013 at 07:37 PM · aimovewaypointto

How to move to random position?

I am working on a AI controller and I am now on the Wandering AI part. How it works is if the AI does not have a destination, create one(Which is a EmptyGameObject), look at it, and move to it. (Everything Works fine)

The "Point" is a prefeb and when created only moves about the Z ans X axis. (I am on a plane at the momment)

But now what I need is if the AI gets within a certain distance of the destination "Point", move that destination "Point" somewhere else. (Or destroy that point and create a new one.)

How can I do this? Everything I am trying is not working correctly. For example the AI will constantly move along its x axis, or snap to (0,0,0) and move in a very fast circle.

I know everything works because if I disable all code having to due with moving the created "Point" then drag it around with my mouse my AI will look at and move to it just fine.

So what I need is how can I make it to where when the AI comes within a certain range of the "Point" or reaches the "Point" the point will either move to another random location based off it's current location.

Like if the AI is at (10,1,10) add a random.range vector3 to the "Point" and change it to lets say if random.range chose (5,1,2) it would be (15,1,12).

Or destroy the "Point" and create a new one based on the current location of the AI.

How would I accomplish this?

 using UnityEngine;
 using System.Collections;
 
 
 /// <summary>
 /// AI controller. This script governs the behavior of the AI Enemy.
 /// If the AI does not have a destination, it will instantiate a "Point" prefab
 /// (Which is just an empty GameObject) into 3D space within the Random.Range 
 /// parameters, then look at, and move to that point.
 /// </summary>
 
 
 public class AIController : MonoBehaviour {
     
     
     
     //----------Variables Start----------
     
     public         Transform     end;                                //Our end position.
     
     public         Transform     start;                                //Our starting position.
     
     public         GameObject     points;                                //The "Point" prefab.
     
     public         float         waitTime;                            //The time to wait before finding a new 
                                                                 //destination. I haven't got to this yet.
     public         float         rspeed;                                //Our rotation speed;
     
     public         float         speed;                                //Our movement speed;
     
     public         GameObject destination;                            //Just a slot for the current created "Point" prefab.
     
     private     Transform     myTransform;                        
     
     public         bool         weCanMove             = false;        
     
     public         int         myDestinations         = 0;
     
     public         bool         thinkingWhereToMove = false;
     
     private     Vector3     pointPosition         = new Vector3(Random.Range(0,2),0,Random.Range(0,2));
     
     
     
     //----------Variables End------------
     
     
     
     // Use this for initialization
     void Start () 
     {
         //A Quick Reference of our transform.
         myTransform = transform;
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (end) 
         {            
             float dist = Vector3.Distance(end.position, transform.position);            
             print("Distance to other: " + dist); 
             
             
             if(dist <= 2)
             {
                 //PointScript pscript = GameObject.FindWithTag("Point").GetComponent<PointScript>();
                 //pscript.testItOut = true;
                 //Destroy(destination);
             }
         }
         
         
         if(myDestinations <=0)
         {
             CreateADestination();
         }
         
         if(myDestinations == 1)
         {
             MoveToDestination();
         }
         
         //Creating our destination. I appologize for this part is poorly written.
         if(thinkingWhereToMove)
         {
             weCanMove = true;
             if(weCanMove)
             {
                 //Instantiate points at mytransform + the value of "position" variable, with our rotation.
                 Instantiate(points, myTransform.position+pointPosition , Quaternion.identity);
             }    
             
             //There is already a destination, prevent any extras being created.
             thinkingWhereToMove = false;
             weCanMove = false;
             
             //Here is where I would add a waitTime.
             //Now we need a new destination.
             myDestinations = 1;
         }
     }
     
     
     void CreateADestination()
     {
         thinkingWhereToMove = true;
     }
     
     void MoveToDestination()
     {
         //This is the translation towards the "Point" part.
         var qtarget = Quaternion.LookRotation(end.transform.position-transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation,qtarget,rspeed*Time.deltaTime);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
     }
 }


And the PointScript:

 using UnityEngine;
 using System.Collections;
 
 
 /// <summary>
 /// Point script. This script only gets the AIController of it's target
 /// and sets the target's "end.position" to the point's position.
 /// Allowing the AI to move towards it.
 /// 
 /// I know this is a bad way to go about this but since 
 /// for now there is only one enemy I am not worried about it.
 /// </summary>
 
 
 public class PointScript : MonoBehaviour {
     
     //----------Variables Start----------
     public        bool         testItOut;            //This is just a quick bool for testing.
     public         GameObject     thisPoint;
     public         GameObject     target;                //The point's Target.
     private     Transform     pointTransform;        //The point's Transform.
     private     Transform     newTransform;
     private     Vector3     newPosition         = new Vector3(Random.Range(5,10),0,Random.Range(5,10));
     
     //----------Variables End------------
     
     void Awake()
     {
         pointTransform = transform;
         target = GameObject.FindWithTag("Target");
         
         //Set the AIController's end.position to the point's position.
         AIController aiController = target.GetComponent<AIController>();
         aiController.end = pointTransform;
         //aiController.destination = thisPoint;
     }
     
     void Update()
     {
         if(testItOut)
         {
             //AIController aiController = target.GetComponent<AIController>();
             //aiController.end = start;
             //Destroy(pointTransform.gameObject);
             //transform.position = pointTransform+=newPosition;
             //transform.Translate(end.position,newPosition);
             //pointTransform.position = Vector3.Lerp(pointTransform.position, pointTransform.position+newPosition, Time.time);
         }
     }
 }
 
Comment
Add comment · 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 robertbu · Jan 26, 2013 at 07:43 PM 0
Share

Folks will better be able to help you if you post your source.

avatar image JoshMBeyer · Jan 26, 2013 at 07:52 PM 0
Share

Opps thought I did, 1 sec

avatar image JoshMBeyer · Jan 26, 2013 at 08:02 PM 0
Share

Sorry about the confusion of the code, I am in the middle of a few projects at the moment and im kind of jumping inbetween so things are a bit scrambled. The commented out lines of code are things I tried but were not working. If this is too confusing I will reorganize everything and explain my question better.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Setzer22 · Jan 26, 2013 at 07:50 PM

Assuming what you want is to create a random location on the map to move to, you'll need to take some things into account:

First of all, limits, you can't generate a completely random location, the location should be whithn a certain range

Second, height; does the location have to be in a random height as well? I don't think so, you might want to only decide the X and Z coordinates for the new location, and you could get the height with some easy raycasting.

In addition, you'll need to take into account any obstacles the AI might find in its path, but this isn't what you were asking here so I won't dwell much on that.

Once you have your limits set, you have to generate random numbers for the X and Z (maybe Y?) coordinates, from whithin some range. That might be easy if your map is completely square and with no obstacles, but things will start to become more complicated when you work with obstacles and unavaliable locations, you can't just set random coordinates then.

If you don't think you'll need to face those problems, just use some randomly generated numbers and problem's over. For anything more complicated than that, you'll have to work with a node-based system. That is, creating single points in the map, each one in a meaningful location, and then linking them, making it able to create paths (that's called pathfinding). With a node-based map, determining a random node should be kind of trivial (more than doing it with coordinates)

Hope it helped!

PS: I can't provide you any code as your question didn't specify that much on what you wanted to accomplish. As you were told in the comments, poting some code would get more people to answer your question

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 JoshMBeyer · Jan 26, 2013 at 07:56 PM 0
Share

Yes it is planned to only move about the X and Z axis. I have thought of the interference of obstacles and unavailable locations but I am workign with a flat plane right now so I haven't got to all that yet but I already have ideas about it.

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

10 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

Related Questions

Way Point Script Change Group 0 Answers

Why isn't a Ai Waypoint Spawning??? 0 Answers

How Can I write a Click To Move Script? 2 Answers

WayPoints mixed with Raycast 1 Answer

Confused about copying Lists! 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