- Home /

# Adding random noise to a direction vector

Hi guys, this should be a simple one: I have defined this direction vector:

(vector3) direction = targetVector - transform.position; direction.Normalize();

Now, how would one add a random noise to this direction, expressed as min and max ANGLES? Should I convert this vector to an angle in degrees, add the noise and then back to normalized vector?

Thank you for the assist!

i'm not sure what you mean by "noise". Do you mean you want a random amount of inaccuracy applied to your direction?

To further illustrate what is meant to be done here, here is an example. A bullet from a high speed, low accuracy machine gun is being fired. The base direction is 100% accurate, but in order to reflect the low accuracy of each projectile a randomization of their direction is needed.

Currently, the projectiles are moving using:

transform.position += direction * projetileSpeed;

What is needed in this case is a way to add a specific angular modifier to the direction vector.

Whoa, i think you guys are making this tougher that what he needs. if he has allready has his normalized direction why cant he simply do this:

```
Vector3 mydirection = Vector3.right;//<--here is your normalized direction
float inacuracy = 0.9f;//<--closer to 1 is more accurate
mydirection = new Vector3 (
Random.Range (inacuracy,1f) * mydirection.x,
Random.Range (inacuracy,1f) * mydirection.y,
Random.Range (inacuracy,1f) * mydirection.z);
mydirection=mydirection.normalized;
```

Wow! This approach is way simpler and works great. Thank you Toddisarockstar!

**Answer** by Cornelis-de-Jager
·
Jul 17, 2017 at 01:37 AM

Use this function:

```
direction += AddNoiseOnAngle (0, 30);
[...]
Vector3 AddNoiseOnAngle ( float min, float max) {
// Find random angle between min & max inclusive
float xNoise = Random.Range (min, max);
float yNoise = Random.Range (min, max);
float zNoise = Random.Range (min, max);
// Convert Angle to Vector3
Vector3 noise = new Vector3 (
Mathf.Sin (2 * Mathf.Pi * xNoise /360),
Mathf.Sin (2 * Mathf.Pi * yNoise /360),
Mathf.Sin (2 * Mathf.Pi * zNoise /360)
);
return noise;
}
```

This will return a Vector3 whos values are between -1 and 1 inclusive.

This doesn't make much sense. Since you add a vector to another vector the result is a linear combination and has very limited capabilities to actually modify the original vector. The min / max angle won't represent an angle at all. Since you basically just add a random vector to another vector the usage of Sin becomes irrelevant. If you pass in "90" as max value the greatest vector that could be returned is (1,1,1) or (-1,-1,-1). So the generated "noise vector" is just a random vector with max length sqrt(3) and a strange non-linear distribution.

Now adding such a vector to the initial direction has nothing to do with a specific change in terms of "angle".

Remember the orginal direction is (0,0,0). Now if I or 500 to the Y value, (0,1,0) vs (0, 500, 0) it won't matter it will still go up. And you are wrong in one thing. Adding 90 degrees won't ever be able to give your a (-1, -1, -1) vector. Because in the formula 90 degrees will result in Sin(PI/2) which is a quarter of a Sine wave, which is 1 (its peak). Going past higher than 180 and lower than 0 will give negative values.

Note it should be Mathf.PI (PI all upper-case) ins$$anonymous$$d of Mathf.Pi.

**Answer** by GamezAtWork
·
Jul 16, 2017 at 05:46 AM

Maybe to that targetVector, you could add in a small amount of X and Y, in order to make it slightly deviate?

For instance:

targetVector += transform.up * Random.Range(-randAmt, randAmt) + transform.right * Random.Range(-randAmt, randAmt);

With randAmt being a float value that you can set.

Indeed that would add some noise, although it would be difficult to balance. What I'm looking after is something that would make it easy to set a projectile from statistically 100% to 1% accurate.

I'm looking a this post to see if it helps - it appears to be what I'm looking for although I'm super bad at trigonometry:

http://answers.unity3d.com/questions/189724/polar-spherical-coordinates-to-xyz-and-vice-versa.html

**Answer** by Bunny83
·
Jul 17, 2017 at 12:41 AM

So i guess you want something like this, right. I just added the C# translation. You can simply do

```
dir = GetPointOnUnitSphereCap(dir, 10f);
```

This will add randomly up to 10° variation to the given direction.

**Answer** by JulianCallens
·
Jul 17, 2017 at 04:19 PM

Thank you for the function. I implemented as follows: (for some reason I could not use Mathf.Pi).

```
Vector3 AddNoiseOnAngle ( float min, float max) {
float xNoise = Random.Range (min, max);
float yNoise = Random.Range (min, max);
float zNoise = 0;
// Now get the angle between w.r.t. a vector 3 direction
Vector3 noise = new Vector3 (
Mathf.Sin (2f * 3.1415926f * xNoise / 360),
Mathf.Sin (2f * 3.1415926f * yNoise / 360),
0
);
return noise;
}
```

At a first glance this appears to work, but the output angles do not seem to be proportionally related to the noise factor added - adding 30 or 3000 yields pretty much the same result (and the deviation never appears to be greater than 45 degrees).

It turns out I merely forgot to normalize the vector before and after adding the noise:

///////////// direction = targetVector - transform.position; direction.Normalize(); direction += AddNoiseOnAngle (-15, 15); direction.Normalize();

////////////// And only THEN using:

```
Vector3 AddNoiseOnAngle ( float min, float max) {
float xNoise = Random.Range (min, max);
float yNoise = Random.Range (min, max);
float zNoise = 0;
// Now get the angle between w.r.t. a vector 3 direction
Vector3 noise = new Vector3 (
Mathf.Sin (2f * 3.1415926f * xNoise / 360),
Mathf.Sin (2f * 3.1415926f * yNoise / 360),
0
);
return noise;
}
```

Thank you Cornelis!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Rotate around a locally tilted axis?, 1 Answer

GameObject Rotation is always postive 0 Answers

Syncing Position and Rotation data in ECS tests? 0 Answers

My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers