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
2
Question by MadJohny · Jul 26, 2014 at 03:47 PM · c#2dphysicsrigidbody2darrow

Rotate towards velocity (2D)

Hi, I want to simulate arrow physics in my game, something minecraft-ish style would be good (when you throw the arrow straight up, when the velocity gets negative because of gravity it rapidly turns down), I can't find any answer to this for 2d games, I want it to only rotate around the z axis, basically instead of making the projectile go forward but barely rotating, I want it to curve depending on the velocity. Any idea on how to do this?

Thanks in advance.

edit: center of mass isn't helping

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
12
Best Answer

Answer by robertbu · Jul 26, 2014 at 05:44 PM

If your arrow is a 3d object constructed so that the point looks at positive 'z' when the rotation is (0,0,0), then the 3D solutions you find (setting the LookRotation() to the velocity) will work in 2D.

If you are using a sprite for your arrow, then construct the sprite so the point of the arrow is to the right (x axis) with rotation is (0,0,0). Then you can do something like:

 Vector2 v = rigidbody2D.velocity;
 angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Comment
Add comment · Show 6 · 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 MadJohny · Jul 26, 2014 at 05:56 PM 1
Share

Could you try to work out some solution that works with the x axis as forward? I know it sounds silly, but I like to keep the way they're supposed to be, like if sprites usually use the x axis as "forward", I like to always use the x axis as forward ins$$anonymous$$d of creating like an empty gameobject just so that the rotation works with z as forward.

avatar image robertbu · Jul 26, 2014 at 06:54 PM 1
Share

The code I listed is for a sprite with the forward pointing to the right. I did not post the 3D code. I changed the wording to make it clearer.

avatar image MadJohny · Jul 26, 2014 at 07:15 PM 0
Share

Okay then, when i looked at the code that seemed like it, but the wording confused me a little bit, but from what I tested it was acting weird

edit: nvm it rotates quite nicely, I might lerp it though, Thanks!

avatar image haim96 · Jul 26, 2014 at 07:37 PM 0
Share

thanks! vote up for helping me as well. :)

avatar image fishghost · Apr 21, 2015 at 06:02 AM 0
Share

I spent so much time trying to rework the 3D version to no avail. Thank you so much.

Also, I added a bool ArrowActive so that the arrow only arcs when it's in the air. Then toggle the value in OnCollisionEnter2D.

Show more comments
avatar image
0

Answer by GameTrainee · May 07, 2016 at 03:47 PM

Vector2 v=GetComponent().velocity; angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

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

24 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

Related Questions

Set player and enemy RigidBodies so that neither can push the other 2 Answers

How to make object bounce from one bound to another? 0 Answers

I am trying to launch a 2d object toward the mouse. 1 Answer

How to check if my enemy hits the ground at a certain velocity then add explosive force. 1 Answer

2D walls affect my jump height 1 Answer


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