Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by asimov · Jun 13, 2016 at 02:04 PM · movementrigidbodyinputaddforce

Change direction of rigidbody when force is no longer being applied

Hi,

I'm creating a basic spaceship style movement system and and am moving the object using AddForce applied to its Rigidbody when a 'thrust' key is held down. The direction of the force is based on the forward direction of its camera, in First Person which is controlled by the mouse (i.e. it moves in the direction it is looking). The force is only applied when the thrust key is pressed down, which means that if the mouse is moved around after the key is released, the ship will continue to move in the direction in which the force was applied, not where it is currently looking. In basic terms:

 shipRB.AddForce(cameraTrans.forward * ThrustSpeed); 

I was wondering how I might be able to change the direction of the Rigidbody and have it turn/rotate and continue to move in the direction the camera is looking in, even when the key has been released.

Any help would be great - thanks in advance.

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
0

Answer by Dibbie · Jun 14, 2016 at 07:36 AM

In your Update, you could try checking if the rigidbody's magnitude is less then your thrust speed. if(shipRB.velocity.magnitude <= ThrustSpeed){//make magic happen}

Comment
Add comment · 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
0

Answer by asimov · Jun 14, 2016 at 10:14 AM

Hi Dibbie - thanks for the suggestion!

But what is the magic that I need to happen to make the Rigidbody move in the direction that the camera's forward is pointing in after a force has been applied? :D

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 Dibbie · Jun 14, 2016 at 06:05 PM 0
Share

Well, it depends on how you want to handle your speed, but one easy way would be something like shipRB.velocity = -Camera.main.transform.forward * Time.deltatime; which when you specify an object (like the camera)'s transform, you can access that objects directional variables such as the cameras "forward", ins$$anonymous$$d of Unity's "forward", and can apply that Vector3 to the velocity of your Rigidbody, which takes a Vector3 - the negative in front specifies, "move in the cameras REVERSE forward" and the opposite of forward is backward.

And if youd want it to gradually reduce speed and then start moving back (like cars do in GTA when you all of a sudden start trying to reverse, it looses all momentum first before going backward), then ins$$anonymous$$d of =, you would use += or -=.

You could also apply the same logic with Translate, or altering the position of your player specifically, etc... Theres tons of way you could do what your trying to achieve.

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

59 People are following this question.

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

Related Questions

Ello~ I need help with transform.parent = null; When I play it and press the DropWeapon input button it goes in a random location and doesnt move . 0 Answers

Jumping cancels out x and z velocity. 0 Answers

Is there a way to "rotate" the direction of addForce (without using addRelativeForce)? 0 Answers

Rigidbody.AddForce moves in a curve 1 Answer

Acceleration only increasing when I press a key 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