# How do I randomize the movement & rotation of a 2D game object, such that it automatically moves in the direction it faces and not walk diagonally ?

I’m spawning them from a game object that ping pongs, so on spawn, they should start moving randomly.

I worked on the code some more but was still not able to piece it. With some help from Maximilian from PushyPixels here is the code that works the best for me. Very simple and precise.

Here is the code for your use:

``````using UnityEngine;
using System.Collections;

public class RandomMovement : MonoBehaviour {

public float rotationSpeed;
public float movementSpeed;
public float rotationTime;

void Start()
{
Invoke("ChangeRotation",rotationTime);
}

void ChangeRotation()
{
if(Random.value > 0.5f)
{
rotationSpeed = -rotationSpeed;
}
Invoke("ChangeRotation",rotationTime);
}

void Update() {

transform.Rotate (new Vector3 (0, 0, rotationSpeed * Time.deltaTime));
transform.position += transform.up*movementSpeed*Time.deltaTime;

}
}
``````

Have a look at something like randomizing the x movement of the 2d sprite. Unity - Scripting API: Random.Range

That docmentation should basically answer your question. Just put in the area threshold that you want the object to spawn in. And of you go ;o

In short, you need a combination of Transform.LookAt and Transform.Translate

That said, AI is something difficult and needs to be tailored to specific situations.
Can you give me your current code for the things that should move?

Updated with scripts!

Okay it’s alot, but here we go:
Create a plane sized 20,0.1,20 > set it’s position to 10,0,10
Create a box and call it “Ai Node”
attach this script to the Ai Node:

``````var worldSize : int = 20;
var oldPos : Vector 3 = aiNode.position;

function Start(){
InvokeRepeating("NodeMove",0,0.1);
}

function NodeMove(){
transform.position.x = Random.Range(0,worldSize);
transform.position.y = 0.55;
transform.position.z = Random.Range(0,worldSize);
}
``````

then create a box sized 0.1,0.1,0.1 and call it randomAi.
Attach this script:

`````` // villager AI FB Projecten || Fabian Broekhuizen

var target : Vector3;
var targetSet : boolean;
var position : Vector3;
var distance : float;

var aiNode : Transform;

var moving : boolean;
var wandering : boolean;
var arrived : boolean;

Start(){
InvokeRepeating("OffTrack",0,2)
}

function Awake(){
wandering = true;
}

function Update(){
aiNode = GameObject.Find("Ai Node").transform;
distance = Vector3.Distance(target, position);
position = transform.position;
transform.position.y = 0.1;

if (moving){
transform.Translate(Vector3.forward / 10 * Time.deltaTime);
targetRotation = Quaternion.LookRotation (target - transform.position);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, 1);
}

if (distance  < 1 ){
arrived = true;
targetSet = false;
}

if (wandering && !targetSet){
target = aiNode.position;
targetSet = true;
moving = true;
}
}

function OffTrack(){
oldPos = aiNode.position;
targetSet = false;
wander = false;
target = transform.position + Random.insideUnitSphere * 5;
yield WaitForSeconds(1);
wander = true;
target = oldPos;
targetSet = true;
}
``````

Updated with request