- Home /
How to gradualy decrease all rigidbody added forces?
I am beginner and 3d artist! I making Space Shooter game in JavaScript with camera from bird-view, spaceship can turn around with "AddForce" floating freely in space and i want to decrease all rigidbody added forces on "Input.GetKey" function, but i dont have idea how to :/ this is my code:
var speed : float = 10;
var rotationSpeed: float = 10;
var sideForce : float = 20;
var topSpeed : float = 20;
function Update()
{
//MOVEMENT...
if(Input.GetKey("s") || Input.GetKey("0"))
{
rigidbody.AddRelativeForce(Vector3.back * speed * Time.deltaTime);
}
if(Input.GetKey("w") || Input.GetKey("1"))
{
rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
}
if(Input.GetKey("q") || Input.GetKey("0"))
{
rigidbody.AddRelativeForce(Vector3.left * sideForce * Time.deltaTime);
}
if(Input.GetKey("e") || Input.GetKey("0"))
{
rigidbody.AddRelativeForce(Vector3.right * sideForce * Time.deltaTime);
}
if(Input.GetKey("a"))
{
transform.Rotate(Vector3.down * rotationSpeed * Time.deltaTime);
}
if(Input.GetKey("d"))
{
transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
}
//unsuccessful attempts to make braking sistem on spaceship (speed slowing down)
if (rigidbody.velocity.magnitude > topSpeed)
rigidbody.velocity = rigidbody.velocity.normalized * topSpeed;
transform.eulerAngles.x=0;
transform.eulerAngles.z=0;
transform.position.y=0;
if(Input.GetKeyDown(KeyCode.LeftShift))
{
rigidbody.velocity = Vector3.zero;
}
}
Answer by Kiwasi · Jul 16, 2014 at 07:04 AM
You could cheat and simply increase rigidBody.drag
yes that code can be used to slow down an object very roughly, BUT after slowing down max speed is decreesed and cannot go back to normal speed. it working just like an Parachute. Is there any other way for decressing add force on current object going direction? :/
Just set the drag back to normal when the key is released...
The other options are
Add a force in the opposite direction to the current velocity vector
$$anonymous$$anually adjust velocity. Be cautious with this approach, you need to do a bit of maths to make your physics look believable this way.
Just add force on the contrary direction, aka Drag.
There is a key difference between increasing drag and applying a force opposite to velocity.
Drag applies a force proportional to the current velocity. As the object slows down the breaking force decreases. This is similar to air resistance or friction.
On the other hand a manually applied force can be of a set magnitude, independent of the velocity. This is more similar to firing braking rockets on a spacecraft.
oou, thats too complicated for me, i think it can be simpler, i just started progra$$anonymous$$g, i want just some simple function to add opposite force to current spaceship direction.... can you write me that code please :/
Going off what Bored$$anonymous$$ormon said,
(btw this script is C# just so ya know).
public class PutTheNameOfExistingScriptHere : $$anonymous$$onoBehaviour {
public float defaultDrag;
public float brakeSpeed;
public GameObject player;
Rigidbody rbody
void Start ()
{
rbody = player.GetComponent<Rigidbody>();
}
void Update()
{
$$anonymous$$ovement();
}
void $$anonymous$$ovement()
{
if (Input.Get$$anonymous$$ey("left ctrl"))
{
rbody.drag = brakeSpeed;
}
if (Input.Get$$anonymous$$eyUp("left ctrl"))
{
rbody.drag = defaultDrag;
}
}
}
That's the bare bones of what you need for drag braking system. You'll need to play with the variables see what suits you and add your own propulsion/movement system.
But I hope it helps ;3