Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Question WindSpeed float needs to be changed with Player direction

Discussion in 'Scripting' started by Andreas12345, Dec 3, 2022.

  1. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    523
    I have in my Game calculations with WindSpeed.
    Code (CSharp):
    1. realWindSpeed_kmh
    as a float
    i set this random at the start between -20 and +20
    So i have Wind :)
    Now the player moves in a direction - North, South, West, East.
    When i start the game the player moves with 15 KmH front Wind for example in direction north.
    When the player turns to direction South, the wind cannot be 15Km/h now it should be -15Km/H
    The direction of the player i can get with:
    Code (CSharp):
    1. playerTransform.eulerAngles.y;
    How can i calculate the WindSpeed with the current direction?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You'll want to represent the windspeed as a Vector3. It is a vector because it has magnitude and direction, not just magnitude.

    Then you can easily convert the wind vector into the player's local coordinate system with:
    https://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html

    If you want a single number representing how fast the wind is in the the player's forward direction, you'd use the dot product on the wind vector and your player's forward direction:
    transform.forward
    . https://docs.unity3d.com/ScriptReference/Vector3.Dot.html (this would be how to get the -15 from your original question).
     
    Andreas12345 likes this.
  3. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    523
    I do know with som ehelp:
    Code (CSharp):
    1. public void CalculateWind()
    2.     {
    3.         Vector3 forward = playerTransform.forward;
    4.         Vector3 windDirection = windig.direction;
    5.         float windStrength = -Vector3.Dot(forward, windDirection) * windig.strength;
    6.         bicycleController.windSpeed = windStrength;
    7.         Debug.Log("Windig" + windStrength.ToString("F0"));
    8.     }