Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 felix330 · Aug 17, 2014 at 03:43 PM · rotationrigidbody2dphysics2d

How to keep the velocity of a rigidbody2d while changing its direction according to rotation?

For the last few days I've been trying to have a rigidbody2d rotate while keeping its current velocity only changing where the velocity is directed. After some research, this is what I've come up with:

             var v:float;
             v = rigidbody2D.velocity.magnitude;
            
            var cos=Mathf.Cos(gameObject.rigidbody2D.rotation);
            var sin=Mathf.Sin(gameObject.rigidbody2D.rotation);
            
            var unitvector : Vector2;
            unitvector = Vector2(cos,sin);
            unitvector=unitvector.normalized;
            
            var x : float;
            var y : float;
            
            x=unitvector.x;
            y=unitvector.y;
            
            gameObject.rigidbody2D.velocity=Vector2(x*v,y*v);   


However, this only makes the object stand still in the air slighty wiggling back and forth when I press the key. What's wrong with my solution and how can I fix it?

Comment
Add comment · Show 2
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 Scribe · Aug 17, 2014 at 03:47 PM 0
Share

how are you rotating your rigidbody? If you know that a specific axis of the rigidbody should always be the direction of the speed then you could do (assu$$anonymous$$g you want it to go forward):

 v = rigidbody2D.velocity.magnitude;
 //apply some sort of rotation
 rigidbody2D.velocity = transform.forward*v; //change this from forward if something else should be
avatar image felix330 · Aug 17, 2014 at 04:07 PM 0
Share

I'm rotating it with addtorque. Using up ins$$anonymous$$d of forward (since it's 2D space) this actually works perfectly and is obviously way simpler than what I tried to do. Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 17, 2014 at 03:45 PM

If you want to keep the velocity constant no matter what direction it is facing or turning, add the following line in FixedUpdate():

  rigidbody2D.velocity.magnitude = rigidbody2D.velocity.magnitude.normalized * someSpeed;
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

In unity 2D c# how to rotate an object like geometry dash? 1 Answer

Arm passing through objects (Rotation - Rigidbody2D) 0 Answers

Rotate object based on projectile impact 0 Answers

setting transform physics2d issue 0 Answers

2D rigidbody velocity relative to rotation? 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