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

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

,Doesn't stop adding force to enemy 0 Answers

2D : How to overwrite rigidbody velocity when "skill" input pressed ??? 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