- Home /
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
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);
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.
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.
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!
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.
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));
Your answer
Follow this Question
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