- Home /
jetpack: breal when no button is pressed?
hey, i did this pretty simple jetpack script:
 using UnityEngine;
 using System.Collections;
 
 
 public class Jetpack : MonoBehaviour 
 {
     public float upthrust;
     public float forwardthrust;
     public Rigidbody rb;
 
 
 
     void FixedUpdate() 
     {
         if  (Input.GetKey(KeyCode.UpArrow)){
             
             rb.AddForce(transform.forward * forwardthrust);
 
         }
 
         if  (Input.GetKey(KeyCode.DownArrow)){
 
             rb.AddForce(transform.forward * -forwardthrust);
 
         }
 
         if  (Input.GetKey(KeyCode.RightShift)){
             rb.AddForce(transform.up * upthrust);
 
 
         }
         if  (Input.GetKey(KeyCode.RightControl)){
             rb.AddForce(transform.up * -upthrust);
         
 
         }
 
         if  (Input.GetKey(KeyCode.LeftArrow)){
             rb.AddForce(transform.right * -upthrust);
 
 
         }
 
         if  (Input.GetKey(KeyCode.RightArrow)){
             rb.AddForce(transform.right * upthrust);
 
 
         }
 
     }
 }
but what i wanna add now (which i don't know how to do this) is, that the jetpack slowly stops when i dont press any button. cause at the moment is just keeps floating around/accelarates
Answer by LK84 · Aug 31, 2016 at 05:18 PM
The easiest way to decrease the velocity of a rigidbody is to increase its drag. So just do something like this in FixedUpdate():
      if (!Input.anyKey) rb.drag=20;
      if (Input.anyKey) rb.drag=0;
Note I just used an arbitrary value for the drag. You have to figure out which one suits you best.
hey, thanks! that works pretty good! is there also a way to make him stop smoother?
Actually the drag force is proportional to the square of the velocity, meaning with decreasing velocity it's decreasing too, so I should be possible to make it look smooth with the right values for the drag. $$anonymous$$aybe ins$$anonymous$$d of the method suggested above keep the drag coefficient constant at a realistic value and it will slow down by itself as soon as no force is added to the rigidbody. I think you just have to experiment a little bit with the drag value
Your answer
 
 
             Follow this Question
Related Questions
Need help on my script to calculate gravity for a mass 1 Answer
Can't Get gravity function to work 1 Answer
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
Correcting Directional velocity for a plane 0 Answers
physics based path system? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                