Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jure006 · Jul 16, 2014 at 07:00 AM · addforcespaceshooterdecrease

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;
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Kiwasi · Jul 16, 2014 at 07:04 AM

You could cheat and simply increase rigidBody.drag

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jure006 · Jul 16, 2014 at 09:10 AM 0
Share

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? :/

avatar image Kiwasi · Jul 16, 2014 at 09:13 AM 0
Share

Just set the drag back to normal when the key is released...

The other options are

  1. Add a force in the opposite direction to the current velocity vector

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

avatar image Paulo-Henrique025 · Jul 16, 2014 at 09:25 AM 0
Share

Just add force on the contrary direction, aka Drag.

avatar image Kiwasi · Jul 16, 2014 at 09:31 AM 0
Share

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.

avatar image jure006 · Jul 16, 2014 at 03:52 PM 0
Share

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 :/

avatar image Firestream1014 jure006 · Dec 16, 2017 at 07:17 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Weapon System Implementation. 1 Answer

AddForceAtPosition not working? 1 Answer

Performance view over spawn waves method 0 Answers

asteroids make framerate dip after too many have been created 1 Answer

How do I get my spaceship to rotate 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges