- Home /

# Calculating a value based on a weighted element within bounds?

I'm trying to write a script that will allow a game object to move in a general direction within specified bounds rather than in a specific direction. Based on what I've seen with the function LerpAngle, the angle portion won't be particularly difficult I think. It works by using a minAngle and maxAngle and interpolating between them based on a value "t" that must be between 0 and 1 inclusively. If t is 0.5, it will return the exact midpoint of the two angles; if it is 0, LerpAngle returns minAngle, and if it is 1, LerpAngle returns maxAngle.

I'll have the ideal vector (calculated elsewhere and already working) which will be identical to the midpoint, and I'll be able to treat minAngle and maxAngle as a +/- threshold from this ideal midpoint.

What I'm trying to do is programatically calculate a "t" value based on an "amplitude" variable.

When the amplitude is 0, t is calculated completely randomly. IE values should be effectively indistinguishable from the performance of t = Random.Range(0f,1f);

When the amplitude is 1, t = 0.5 no matter what.

When it is any value in between 0 and 1, I'd like it to sort of be a gradient that plots like a progressively more narrow bell curve from 0 to 1.

Rough sketch of expected performance -

I'm not really sure how to implement this, but it seems like something that might already have a library function somewhere - if not necessarily unity. Can anyone either point me in a helpful direction for writing it myself, or towards an existing function that does something similar?

**Answer** by Akkronym
·
Oct 08, 2015 at 03:58 AM

With the help of a friend I was able to figure out that the concept I'm referring to here is Normal Distribution and Random Gaussian Variables.

I don't have a working implementation yet, but based on what I've found so far a "Box-Muller Transformation" keeps coming up and seems like an effective direction to go in.

Hopefully this will help point anyone else with this issue in the right direction as well.

### 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

How can I detect non-rectangular bounds? 0 Answers

Calculate weighted mean based on direction 0 Answers

How to 'Lerp' random local vectors? (Solved!) 3 Answers

How do I smoothly shift between Layer Weights 1 Answer

Vector3 Lerp mis-match 0 Answers