Search Unity

preventing value from going lower than another

Discussion in 'Scripting' started by Hubardo, Jul 31, 2015.

  1. Hubardo

    Hubardo

    Joined:
    Apr 2, 2015
    Posts:
    22
    i have a script responsible for rotating a boat relative to how fast it is going, the faster it goes the smaller the rotation is.
    so im dividing my rotationSpeed by the currentPower, and its working fine.
    problem is, when the currentSpeed value is at its lowest it rotates faster than i would like, i want to set a minimum rotation speed to prevent it from happening, but still keep it transitioning smoothly between those values

    heres the relevant part of the script
    the minimum speed is when currentPower == 0f

    Code (CSharp):
    1. //Turning boat
    2.         if (currentPower == 0f)
    3.         {
    4.             float rotation = Input.GetAxis ("Horizontal") * stationaryRotationSpeed;
    5.             rotation *= Time.deltaTime;
    6.             transform.Rotate (0, rotation, 0);
    7.         }
    8.         else if (currentPower != 0f)
    9.         {
    10.             float rotation = Input.GetAxis ("Horizontal") * rotationSpeed / currentPower;
    11.             rotation *= Time.deltaTime;
    12.             transform.Rotate (0, rotation, 0);
    13.         }
     
  2. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    You could just check for when the power drops below the acceptable value that makes it turn too fast, lets say thats 5.

    Code (csharp):
    1.  
    2. // you dont want to change the value of currentPower
    3. // so use a temporary variable
    4.  
    5. if (currentPower <5)
    6. {  rotationPower = 5; }
    7. else
    8.   rotationPower = currentPower;
    9.  
    10. // Then just use the code from your second block.
    11.  
    12. float rotation = Input.GetAxis ("Horizontal") * rotationSpeed / rotationPower;
    13. rotation *= Time.deltaTime;
    14. transform.Rotate (0, rotation, 0);
    15.  
    16.  
     
    Last edited: Jul 31, 2015
  3. Hubardo

    Hubardo

    Joined:
    Apr 2, 2015
    Posts:
    22
    man, oh man, that was pretty easy after all :) somehow i completely missed this solution

    thanks a bunch, only problem i have now is transitioning between the stationaryRotationSpeed (witch is set to the minimum speed) to the new rotationPower speed, and vice versa.

    if i go from currentPower 1 to 0, than my rotation speed changes in an instant from its fastest to its slowest, any ideas how can i smooth this transition?

    *edit - nvm, went a different way with this, thanks
     
    Last edited: Jul 31, 2015
  4. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    736
    OK, good luck!