Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Reduce banking with speed

Discussion in 'Scripting' started by KhanTyranitar, Jun 30, 2007.

  1. KhanTyranitar

    KhanTyranitar

    Joined:
    Feb 20, 2006
    Posts:
    67
    The vehicle is supposed to inverse bank during turns. This part works, but for realism, I want it to bank less when the vehicle is moving faster. I am not that familiar with Unity scripting overall, and I am not quite sure how to do this. I am including the code pertaining to this function below. If any of you guys could suggest what I need to do, to create this behavior, I would be in your debt.

    var forwardforce = Input.GetAxis ("Vertical");
    var shiprotation = Input.GetAxis ("Horizontal");
    var reverseforce = Input.GetAxis ("Vertical");


    // Calculate the forward motion
    forwardforce = enginepower * forwardforce;
    reverseforce = reversepower * reverseforce;


    // This prevents the ship from moving backwards too quickly
    if (forwardforce <= reversepower) forwardforce = reverseforce;


    rigidbody.AddRelativeForce (0,0,forwardforce);

    // Calculate the rotational forces;
    shiprotation = turnrate * shiprotation;

    rigidbody.AddTorque (0, shiprotation, 0);

    var shipbanking = shiprotation *-1;




    playership.localEulerAngles = Vector3 (0,0, shipbanking);
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    How about when you compute shipbanking you multiply by a factor that you compute using the magnitude of the the velocity of the rigidbody?
     
  3. KhanTyranitar

    KhanTyranitar

    Joined:
    Feb 20, 2006
    Posts:
    67
    Ok, but my question is, how do I do this? I know what I want to do, but my efforts were not giving me good results.
     
  4. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    You can do something like
    Code (csharp):
    1.  
    2. // Appears in Inspector for tweaking. Higher the value the more speed dampens banking.
    3. var bankFactor = 0.07;
    4. ...
    5. bankModifier = 1.0 - (rigidbody.velocity.magnitude * bankFactor);
    6. var shipbanking = shiprotation *-1 * bankModifier;
    7. ...
    8.  
     
  5. KhanTyranitar

    KhanTyranitar

    Joined:
    Feb 20, 2006
    Posts:
    67
    Ok, thank you, that did exactly what I needed it to do. I wouldn't have thought of that, at least not very quickly.