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
0
Question by jure006 · Jul 17, 2014 at 12:27 AM · movementaddforceshootership

How to add opposite force of current direction?

I making Space shooter game, camera looking from bird view, my space ship turning around and goes in all direction by AddForce fucntion. i want to add braking sistem to decresing addforce in current direction until it reach zero. :) can you write me that code?

this is my script

 var speed : float = 10;
 var rotationSpeed: float = 10;
 var sideForce : float = 20;
 var topSpeed : float = 20;
 
 function Update()
 {
     //Controls for ship...
     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);
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by rutter · Jul 17, 2014 at 12:35 AM

The rigidbody has a velocity vector indicating the direction and speed of its current movement.

If you want another vector pointing in the opposite direction? Just multiply by negative one. :)

 var opposite = -rigidbody.velocity;
 rigidbody.AddForce(opposite * Time.deltaTime);

Maybe you want to control how hard you brake?

 var opposite = -rigidbody.velocity;
 var brakePower = 10;
 var brakeForce = opposite.normalized * brakePower;
 rigidbody.AddForce(brakeForce * Time.deltaTime);

The above might end up pushing backwards, when you're almost stopped.

You could also scale down velocity:

 var curSpeed = rigidbody.velocity.magnitude;
 var newSpeed = curSpeed - 10 * Time.deltaTime;
 if (newSpeed < 0) {
     newSpeed = 0;
 }
 rigidbody.velocity = rigidbody.velocity.normalized * newSpeed;

In this specific example, it might be simplest to simulate a brake by increasing the rigidbody's drag:

 if (braking) {
     rigidbody.drag = 20;
 } else {
     rigidbody.drag = 5;
 }

If any of this confuses you, I suggest reading up on the Vector3 and Rigidbody manual pages.

Comment
Add comment · Show 3 · 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 17, 2014 at 06:04 AM 0
Share

yeea, very interested codes. That 2nd code is exactly what i needed, you solved my problem, thank you man so so much! :)

avatar image tcz8 jure006 · Sep 03, 2020 at 03:59 AM 0
Share

Good code indeed, Thank you! @jure006 you should accept his answer, he deserves it.

avatar image Shishigami · May 22, 2017 at 02:11 PM 1
Share

Is there a way to convert this into c#? I dont really have that much understanding of Javascript, but i have the exact same problem. :P

avatar image
1

Answer by Shishigami · May 24, 2017 at 07:44 AM

Is there a way to convert this into c#? I dont really have that much understanding of Javascript, but i have the exact same problem. :P

Comment
Add comment · Show 1 · 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 Hamilcar-games · Aug 18, 2018 at 02:12 PM 1
Share

its c# just add in start method getcomponent rigidbody

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I calibrate phone accelerometer? 0 Answers

Prevent Rigidbody From Rotating 3 Answers

Roll a ball camera problem? 1 Answer

Smooth 3D Dodge Roll Animation 0 Answers

Ship movement in space-based RTS 0 Answers


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