• 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 Ozwig · Nov 19, 2015 at 10:07 PM · rigidbody2daddforceaddtorque

Rigidbody2D Flight

Hello all,

Been working on this little project with a Rigidbody2D rocket, however, the flight of the rocket is not working quite how I would like it to. I would like the rocket to always move in the direction it is facing, like a jet. As it is now, it kind of Tokyo drifts round corners. This video maybe explains it better:

https://www.youtube.com/watch?v=lJxldkuWuV8

If anyone could shed some light on how to code this I would be most grateful!

Also see images for what I am trying to achieve.

Many thanks in advance!

     public float velocity;
     public float leftRotationSpeed;
     public float rightRotationSpeed;
     
 
 
     void FixedUpdate() 
 
     {
         if (Input.GetKey (KeyCode.UpArrow)) 
         {
             GetComponent<Rigidbody2D>().AddForce(transform.up * velocity);
         }    
 
         if (Input.GetKey (KeyCode.DownArrow)) 
         {
             GetComponent<Rigidbody2D>().AddForce(transform.up * -velocity);
         }    
 
 
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             GetComponent<Rigidbody2D>().AddTorque(Input.GetAxis ("Horizontal") * leftRotationSpeed);
         }
 
         if (Input.GetKey (KeyCode.RightArrow)) 
         {
             GetComponent<Rigidbody2D>().AddTorque(Input.GetAxis ("Horizontal") * rightRotationSpeed);
         }
 
     }
 
 }![alt text][1]


[1]: /storage/temp/58457-rocket2.jpg

rocket2.jpg (225.1 kB)
rocket1.jpg (227.5 kB)
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
1

Answer by Dinosaurs · Nov 19, 2015 at 10:14 PM

Rather than adding torque, it sounds like you should just rotate the object. That will make the rotation instant rather than slowly building force.

http://docs.unity3d.com/ScriptReference/Rigidbody2D.MoveRotation.html

Also, you should store the Rigidbody2D in a variable rather than calling GetComponent() every FixedUpdate(). Repeated calls to GetComponent are very expensive and will likely cause performance issues down the line.

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 Ozwig · Nov 19, 2015 at 10:21 PM

Thanks @Dinosaurs! Though I do quite like the slight physics lag you get with torque. Do you think I can achieve the same effect with .MoveRotation?

Comment
Add comment · Show 4 · 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 Dinosaurs · Nov 19, 2015 at 10:33 PM 0
Share

Yes, there are many ways you could achieve that effect. One way might be to store the current rotation speed in a variable and use $$anonymous$$athf.Lerp() to slowly increase it to a maximum rotation speed before applying the rotation.

In the future if you want to follow up on an answer, please leave a comment rather than creating a new answer. And if an answer solves your issue, press the checkmark to accept it so others know it's a working solution.

avatar image Ozwig Dinosaurs · Nov 20, 2015 at 11:51 AM 0
Share

Thanks again. I have now changed from AddTorque to $$anonymous$$oveRotation and the effect is the same (the rocket travels sideways round corners due to its momentum) but without the lag during rotation. Can you think of any other way I could make it appear to s$$anonymous$$r around obstacles?

$$anonymous$$any thanks for your help.

avatar image Dinosaurs Ozwig · Nov 20, 2015 at 07:20 PM 1
Share

Rather than adding force, set the velocity to the forward vector.

avatar image Ozwig · Dec 03, 2015 at 10:54 AM 0
Share

Only just got round to this now. Setting the Velocity worked perfectly. $$anonymous$$any Thanks @Dinosaurs!

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

35 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

Related Questions

Using rb.velocity and Add Force on the same gameobject? 0 Answers

Instantiated Prefab no Rigidbody2D on Update function 1 Answer

Shooting a bullet that stops in place when mouse is released 0 Answers

AddTorque turning not rotating opposite direction. 2 Answers

2D : How to overwrite rigidbody velocity when "skill" input pressed ??? 0 Answers

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