ridged body slow down but not affect gravity.

hi ive been making a hover car script and its all working well but it must slow down but due to the drag wich makes it slow down, gravity dosent work propoly it floats down realy slowly hear is the script any help would be greatly apreciated:)

#pragma strict
var upforce=0.0;
var upforceidle=0.0;
var maxrotate=0.0;
var dragg=0.0;
var speed : float = 10.0;
var rotationSpeed : float = 10.0;

function Update () 
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) 
{
if (hit.distance < 1)
{
rigidbody.AddForce (Vector3.up * upforce);
}
else
{
rigidbody.AddForce (Vector3.up * upforceidle);
//rigidbody.drag = 0.0;
}
}
//var gas : float = (Input.GetKey("space")?1.0:0.0) * speed ;
var torque : float = Input.GetAxis ("Horizontal") * rotationSpeed;
//var stopping = 0 - gas;

//rigidbody.AddRelativeForce (Vector3.forward * gas);
rigidbody.maxAngularVelocity = maxrotate;

if (Input.GetKey("space")?1.0:0.0)
{
rigidbody.AddRelativeForce (Vector3.forward * speed);
if(Input.GetAxis ("Horizontal"))
{
rigidbody.drag = dragg;
}
else// if (hit.distance < 1.1)
{
rigidbody.drag = 0.1;
}
}
else
{
rigidbody.drag = dragg;
}
if(Input.GetAxis ("Horizontal"))
{
rigidbody.AddRelativeTorque (0, torque, 0);
//rigidbody.AddRelativeForce (-Vector3.forward * gas);
//rigidbody.velocity = Vector3.zero;
rigidbody.drag = dragg;
}
//if (hit.distance > 1)
//{
//rigidbody.drag = 0.01;
// }
}

don’t use drag to slow things down, instead just:

if (notmoving) //you decide when this is true
{
rigidbody.velocity.x *= 0.9;
rigidbody.velocity.z *= 0.9;
}

that should do it (untested). lower number = faster slowdown. Its really quite primitive and should only occur if you’re not colliding and not moving. I would probably do it another way personally, such as adding a breaking force, but the above solution is very usable.

Drag is the totally wrong thing to use. If you must use drag, the mass with the mass of your stuff.

thanks hippo coder this is the script i came up with using your way:

#pragma strict
var upforce=0.0;
var upforceidle=0.0;
var maxrotate=0.0;
var dragg=0.0;
var speed : float = 10.0;
var rotationSpeed : float = 10.0;

function Update () 
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) 
{
if (hit.distance < 1)
{
rigidbody.AddForce (Vector3.up * upforce);
}
else
{
rigidbody.AddForce (Vector3.up * upforceidle);
}
}
var torque : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rigidbody.maxAngularVelocity = maxrotate;

if (Input.GetKey("space")?1.0:0.0)
{
rigidbody.AddRelativeForce (Vector3.forward * speed);
if(Input.GetAxis ("Horizontal"))
{
rigidbody.velocity.x *= 0.99;
rigidbody.velocity.z *= 0.99;
}
}
else
{
rigidbody.velocity.x *= 0.99;
rigidbody.velocity.z *= 0.99;
}
if(Input.GetAxis ("Horizontal"))
{
rigidbody.AddRelativeTorque (0, torque, 0);
rigidbody.velocity.x *= 0.99;
rigidbody.velocity.z *= 0.99;
}
}

Yeah :slight_smile: what you’re doing now is classic 2D physics - its roughly how we always used to do physics for mario style platformers, or shootemups etc - even back on amigas.

You can use the multiply by 0.9x trick for anything you wish to have a nice bit of inertia based slow down to or drag “when you want” :slight_smile: