# Reduce banking with speed

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

1. ### 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;

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

var shipbanking = shiprotation *-1;

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

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

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

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

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.