# How do i put random velocity in a Vector2?

I'm very new to C# scripting.

I want to use a random velocity for my Enemies using Vector2. Here's the code that I have done at the moment:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy1 : MonoBehaviour
{
[SerializeField]
Rigidbody2D rb2d;

[SerializeField]
Vector2 vectorEnemysVelocity;

[SerializeField]
gameManager gameManager;

[SerializeField]
AudioSource source;

[SerializeField]
AudioClip deathSound;

private void Awake()
{
gameManager = FindObjectOfType<gameManager>();
}

void Start()
{
MoveEnemy();
}

void MoveEnemy()
{
rb2d.velocity = vectorEnemysVelocity;
}

private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.layer == 8)
{
gameManager.GenerateGameOver();
source.PlayOneShot(deathSound, 1f);
}

if (collision.gameObject.layer == 9)
{
Destroy(gameObject);
}
}
}


With this I can choose the velocity but as I said earlier I want it to be random. How can I do this?

• Take a look at docs.unity3d.com/ScriptReference/Random.Range.html with an example for a Vector3. From there it should be quite easy for you to apply it to Vector2 Dec 2 '20 at 12:57
• You've been searching for a week and you didn't find the Random class in Unity that gives you a method to generate a random Vector2? Dec 2 '20 at 16:00
• @Zibelas You should post this as an answer. Dec 2 '20 at 16:18
• @DMGregory I've been a developer for years by now, and this is the first time I bump into such a useful function. I'd better dust off the docs more often. Dec 2 '20 at 18:24

There is always more than one way on how to do it.

https://docs.unity3d.com/ScriptReference/Random.Range.html

This gives you a random value between your given min and max value. Gives you a lot of control and is easy to understand.

float minValue = 0f;
float maxValue = 10f;
Vector2 randomVector = new Vector2(Random.Range(minValue, maxValue), Random.Range(minValue, maxValue));


Another method (credit to DMGregory) is https://docs.unity3d.com/ScriptReference/Random-insideUnitCircle.html which gives you directly a Vector2.

float maxValue = 10f;
Vector2 randomVector = Random.insideUnitCircle * maxValue;
Debug.Log(randomVector);

• Note that the first method gives you a vector inside a square, and the second gives you a vector inside a disc. With the second option, you control the maximum length of the vector (speed) directly, and it's the same in all directions. With the first method, diagonal vectors are allowed to be √2 times longer (faster) than the max value you input, so you can go different speeds in different directions. The first method with the numbers given also never generates velocities pointing down or to the left. Jan 2 at 12:49

Another approach is generate a random vector in polar coordinates, and convert it to cartesian coordinates:

public Vector2 InCircle(float radius)
{
// Generate random vector in polar coordinates
var angle = Random.Range(0f, 2f * Mathf.PI);

// Convert to cartesian coordinates
var x = radius * Mathf.Cos(angle);
var y = radius * Mathf.Sin(angle);

return new Vector2(x, y);
}


I have a Unity library with UPM support called Eflatun.RandomUtils that has a bunch of useful RNG utility methods, and one of them covers your case, called InCircle:

// using Eflatun.RandomUtils;

var rng = new BetterRandom();
var result = rng.Vector2.InCircle(radius);

• Note that the polar version you've shown here is biased toward choosing vectors near the center of the circle. That may be desirable for some circumstances (eg. having an equal probability of any speed in the range), but I wanted to mention it because it may not be obvious from the code alone for folks unfamiliar with random sampling. The version in the external link corrects for this by adding a square root to the radius. Jan 3 at 14:28