Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NinjaISV · Jun 29, 2015 at 05:33 PM · physics2dmathtrajectoryarrowbow

How to make an arrow fly realistically.

Hey guys! Now I've seen a bunch of other questions and topics like this on the forums, but none of them work for what I am needing. Basically I wan't an arrow that is shot from a bow to move realistically (The tip of the arrow points in the way it's traveling.) but I can only find code that works for an only-right bow. Here's a picture of what I'm talking about:

alt text

And here's the code I'm using: EDIT: Do NOT use LerpAngle for functions like this. It makes the Rotation go from 360 degrees back to 0 instead of rotating it properly.

 Vector3 velocity = rigidbody2d.velocity;
 float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
 Quaternion tempRotation = Quaternion.AngleAxis(angle, Vector3.forward);
 Quaternion newRotation = gameobejct.transform.rotation;
 
 newRotation.x = Mathf.LerpAngle(newRotation.x, tempRotation.x, speed);
 newRotation.y = Mathf.LerpAngle(newRotation.y, tempRotation.y, speed);
 newRotation.z = Mathf.LerpAngle(newRotation.z, tempRotation.z, speed);
 newRotation.w = Mathf.LerpAngle(newRotation.w, tempRotation.w, speed);

Thanks!

arrow-diagram.png (42.2 kB)
Comment
Add comment · Show 4
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 maccabbe · Jun 29, 2015 at 06:11 PM 0
Share

1) Are you using this code in Update or FixedUpdate?

2) How does this change the objects trajectory? I don't see where you are assigning any of these values back to the rigidbody or object.

To simulate an arrow it should be enough to just simulate gravity. For instance something like

 void FixedUpdate(){
    rigidbody2d.velocity+=gravity*Vector3.down;
    transfrom.rotation=Quaternion.LookRotation(rigidbody2d.velocity);
 }

avatar image NinjaISV · Jun 29, 2015 at 06:29 PM 0
Share

The code is being called in FixedUpdate, and it only changes how fast the arrow rotates depending on where I place the code. Also, I made the code a function that can be called so it's returning the Quaternion, not modifying it. Another thing, the code you gave me is very weird... What is the "gravity" variable and why would I be modifying the velocity of the arrow? I already have a rigidbody2d on the arrow, I don't need to simulate gravity twice. Also, i'f I just use the

transfrom.rotation=Quaternion.LookRotation(rigidbody2d.velocity);

code, it makes my arrow disappear after I shoot it.

avatar image maccabbe · Jun 29, 2015 at 07:21 PM 0
Share

The variable gravity would be a float that represents the acceleration felt due to gravity. For instance, in real life gravity would by 9.8 m/s^2. I assumed that you would be modifying the velocity of the arrow is because this is what happens in real life and what gives arrows a parabolic trajectory.

http://www.physicsclassroom.com/class/vectors/Lesson-2/Characteristics-of-a-Projectile-s-Trajectory

Of course, this is a video game and you are free to make your own physics.

transfrom.rotation=Quaternion.LookRotation(rigidbody2d.velocity); is just supposed to look in the direction the arrow is traveling and is probably disappearing because the arrows forward is not the same same the arrows object forward or the world up is not the same as the direction you want the arrows up to be aligned with. Since this is 2D you can probably just use $$anonymous$$athf.Atan2 to get the rotation along the z axis.

avatar image NinjaISV · Jun 29, 2015 at 07:56 PM 0
Share

Alright, thanks. I've found a solution that works:

 Vector2 velocity = rigidbody2d.velocity;
 float angle = $$anonymous$$athf.Atan2(velocity.y, velocity.x) * $$anonymous$$athf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

I just make sure that the arrow is facing right at (0, 0, 0) and place the code in Update.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by NinjaISV · Jun 30, 2015 at 12:49 AM

Uhuh! I've found a solution that works perfectly! I did a little more research and found this: http://answers.unity3d.com/questions/757118/rotate-towards-velocity-2d.html

Basically this code should work if your arrow is facing right at the rotation (0, 0, 0,).

 Vector2 v = rigidbody2D.velocity;
 angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Comment
Add comment · Show 3 · 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 skater-x · Oct 20, 2017 at 10:13 PM 0
Share

it's works but why the arrow still rotate when he fall in the ground (sorry about my language) help me plz i trying to make a 2d game like tower fall and thank you

avatar image dangalg skater-x · Sep 15, 2018 at 08:04 PM 0
Share

This only points the arrow in the direction of the rigidbody2D velocity vector. If it is spinning it is something you are doing.

avatar image Wollbobaggins · Apr 09, 2020 at 10:35 PM 0
Share

Wow! Great find, NinjaISV. This worked perfect for me!

For anyone who has a projectile at a different orientation than desired, merely multiply the projectile's quaternion by the degrees you want to rotate it by.

Vector3 velocity = GetComponent().velocity;

float angle = $$anonymous$$athf.Atan2(velocity.y, velocity.x) * $$anonymous$$athf.Rad2Deg;

Quaternion myRotation = Quaternion.AngleAxis(angle, Vector3.forward) * Quaternion.Euler(0, 0, -90); // <- I needed to change my launch by 90 degrees

transform.rotation = myRotation;

avatar image
2

Answer by Hellium · Jun 29, 2015 at 06:49 PM

Manipulating the values of quaterions individually is a very, very, very, very, very bad idea, unless you perfectly know what you are doing (and it's not the case obviously).

Unity has a built-il method to do a Slerp between two quaternions simply called Quaternion.Lerp http://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html

Comment
Add comment · Show 3 · 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 NinjaISV · Jun 29, 2015 at 07:07 PM 0
Share

Ahhh. That must have been one of the mistakes I made. Thanks.

avatar image 1instrument · Nov 11, 2015 at 07:37 AM 0
Share

How would you assess the 2nd Quaternion in the case of an arrow shot towards an unknown object? Thanks for the help!

avatar image NinjaISV 1instrument · Nov 11, 2015 at 04:03 PM 0
Share

You mean like make the arrow point at a target ins$$anonymous$$d of the direction it is traveling? This would look very unrealistic and messy. I advise you just add the appropriate amount of force to the arrow to make it travel towards the target, thus making it face the target and travel towards it.

avatar image
0

Answer by gloton2535 · Mar 09, 2019 at 02:36 AM

https://answers.unity.com/questions/14899/realistic-rotation-of-flying-arrow.html maybe could more help

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drawing projectile trajectory 5 Answers

Calculating trajectory with PhysX drag 1 Answer

Calculating 3D Physics Prediction of Shot Direction with Moving target and moving gun (inertia) 1 Answer

Click to Shoot 1 Answer

Arrow calibration with bow and string! 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