• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Rigidbody.AddForce moves in a curve 1 Answer

Jumping cancels out x and z velocity. 0 Answers

Player going through walls 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges