2D Wind Indicator

Hi, I am using a arrow to indicate a Vector2 wind direction which I would like to rotate in the UI to point in the direction of the wind.
How would I rotate a Image of a arrow to point in the direction of the wind?
I have got as far as the code below but this is wrong.

public void GetWindDirection()
    {
        windDir = new Vector2(Random.Range(0f, 360f), Random.Range(0f, 360f));
        windStrength = Random.Range(-0f, 0.1f) / 100;

        Debug.Log("Wind Direction: " + windDir + " Wind Strength: " + windStrength);
    }
//ADD WIND HERE
            myRb.AddForce(windDir * windStrength);
public void UpdateWindIndicator()
    {
        Vector2 windDir = gameMan.GetWindDirection();
        //windIndicator.transform.rotation = windDir;
    }

If your wind direction (and strength) is represented as a Vector2, then you convert that to an angle using Mathf.Atan2, e.g.:

float angle = Mathf.Atan2(windDir.y, windDir.x) * Mathf.Rad2Deg;
transform.localEulerAngles = new Vector3(0, 0, angle);

Depending on which way your display is set up, you might want to rotate around Y instead of Z in the second line above. Also this is off the top of my head, so please double-check the syntax. :slight_smile:

Thankyou Joe, the Image now rotates but there is still a issue with the windDir, I dont think I am calculating the wind properly with the first line of code at the top as the ball behaves oddly when force is applied.
I`m trying get get a random direction and apply it to the Rb if you can see the error.
Many thanks.

Yes, that first line doesn’t make much sense. You’re computing a wind vector that is somewhere in the giant square area between [-360,360] and [360,360]. Perhaps you meant to use Random.onUnitCircle instead?

Or, maybe you want to keep your windDir as just an angle, not a Vector2. Then you wouldn’t have to use Atan2 to recover the angle when you need it later. In that case declare it as Vector2 and just do windDir = Random.Range(-360f, 360f).

Thanks Joe, if windDir is now a float how do I apply it to the rb with myRb.AddForce(dir, str) if Add force is asking for a vector2 for the first parameter?
Many thanks.

Ah, in that case you would need to find the corresponding vector, taking into account both the angle and the strength. Trigonometry is your friend here:

float radians = windDir * Mathf.Deg2Rad;
Vector2 windVec = new Vector2(windStrength * Mathf.Cos(radians), windStrength * Mathf.Sin(radians));

Thanks Joe, ive gone for the simplest method with a float, but with this code the wind indicator doesnt seem to point in the right direction. If you can see the error. Many thanks.

public void UpdateWindIndicator()
    {
        //get winddirection
        float windDirection = gameman.GetWindDirection();
        float angle = windDirection * Mathf.Rad2Deg;
        windIndicator.transform.localEulerAngles = new Vector3(0, 0, angle);
    }

Not from this, no. I see from line 5 that this code assumes GetWindDirection() is going to return a wind direction in radians (rather than degrees). Is that in fact the case?

The wind direction is a float Joe, thanks.
float windDirection = random.range(-360, 360);
Wind Indicator point in direction of wind, thanks.

Hi Joe, can you tell me what line 5 should be if windDir is a float,
float angle = windDirection * Mathf.?
Many thanks.