# 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)
{
}

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

{
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.
///
/// 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);
}
}
}

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