Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # Random movement of a sprite in 2D

• 0
• 9
• 0
• 0
• 3
• 0
• 0
• 0
• 3.4k
Comment on it

When we develop a game sometimes we need gameobject move randomly without any user input. In this script a sprite randomly move in 2D and also rotate in moving direction.

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

public class move : MonoBehaviour
{
public float moveSpeed ;
public Vector3 dir;
public float turnSpeed;
float targetAngle;
Vector3 currentPos;
bool play=true;
Vector3 direction;
void Start()
{
dir = Vector3.up;
InvokeRepeating ("Start1", 0f, 5f);
}
void Start1(){
play = true;
direction = new Vector3(Random.Range(-3.0f,3.0f),Random.Range(-4.0f,4.0f),0); //random position in x and y
}

void Update()
{
currentPos = transform.position;//current position of gameObject
if(play)
{ //calculating direction
dir = direction - currentPos;

dir.z = 0;
dir.Normalize ();
play = false;
}

Vector3 target = dir * moveSpeed + currentPos;  //calculating target position
transform.position = Vector3.Lerp (currentPos, target, Time.deltaTime);//movement from current position to target position
targetAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90; //angle of rotation of gameobject
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, targetAngle), turnSpeed * Time.deltaTime); //rotation from current direction to target direction
}
void OnCollisionEnter2D()
{

CancelInvoke ();//stop call to start1 method
direction = new Vector3 (Random.Range (-3.0f, 3.0f), Random.Range (-4.0f, 4.0f), 0); //again provide random position in x and y
play = true;

}
void OnCollisionExit2D()
{
InvokeRepeating ("Start1", 2f, 5f);
}
}``````

## 3 Comment(s)

This is my solution for circle random movement:

```using System.Collections;
using UnityEngine;
public class RandomMove : MonoBehaviour
{
public float moveSpeed;
Vector3 target;
public float SpeedOfChangingTarget = 5;
private float camHeight;
private float camWidth;
SpriteRenderer spriteRenderer;
void Start()
{
Camera cam = Camera.main;
camHeight = 2f * cam.orthographicSize;
camWidth = camHeight * cam.aspect;

spriteRenderer = GetComponent<SpriteRenderer>();

InvokeRepeating("GenerateNewTarget", 0f, SpeedOfChangingTarget);
}

void Update()
{
Vector3 currentPos = transform.position;
if (currentPos == target)
{
GenerateNewTarget();
}
transform.position = Vector3.MoveTowards(currentPos, target, Time.deltaTime*moveSpeed);//movement from current position to target position
}
void OnCollisionStay2D()
{
GenerateNewTarget();
}
void GenerateNewTarget()
{

}
}
```
• over 4 years ago

Thank you very much, it was much useful

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...