- Home /
Realistic rotation of flying arrow?
The title says it all. How would I realistically simulate rotational physics? For example, I have an arrow. When I shoot it, it should go up and then rotate towards the ground as it's falling. However, when I shoot it now, it'll go up, and then fall back to the ground, all the while maintaining the same rotation. This doesn't look good at all. I thought perhaps drag was the key, but it doesn't fit my lock, so to speak. How would I do this? Thanks.
Answer by KvanteTore · Apr 12, 2010 at 07:10 AM
One way of solving would be to make the arrow out of two rigidbodies, a tip and a tail, connected with a FixedJoint. If you then make the tip heavier than the tail, and/or have a larger drag on the tail, I believe you should get the desired rotating with tip towards ground effect.
It works, but now I have to instantiate an arrowhead as well. I tried putting them both in an empty gameObject, but that didn't work either. Any ideas?
I would love to know how to use this method, but I have the same problem with instantiating two arrow peices. They fall apart even though they are in a prefab.
Late response but this took me a while, this was exactly the same answer I came around to. You'll wan to put a Fixed joint component on the rear "drag" child object and have the rest of the arrow as the "connected body". You then just set the drag of of the child object higher than the other.
Sorry for necroing this, but I've tried this and my arrows rotate the way I want them. However, I'm having trouble with the instantiation of my arrows. If I am not setting their velocity, when the appear on screen, their rotation is the original prefab rotation for like a frame or two before rotating to the desired rotation. When I am setting the velocity though, the arrow stays in the rotation it's in in those two frames. All of this does not happen if I remove the rigidbody. Any ideas why it does this?
Answer by runevision · Apr 12, 2010 at 10:02 AM
The arrow rotates in real life due to wind resistance that pushes on the arrow in a way that is specific to the shape of the arrow. That is overly complicated to simulate "realistically" in a game, so it's much easier to just rotate the arrow such that the tip points in the direction of movement:
myArrow.transform.forward =
Vector3.Slerp(myArrow.transform.forward, myArrow.rigidbody.velocity.normalized, Time.deltaTime);
Agree. This is probably the easiest solution. If the arrow can change velocity abruptly (e.g. hit a wall and then fall straight down), it could possibly be improved by lerping the orientation towards the orientation.
Good point. I changed the example code so it slerps the direction.
That's a great idea. However, it didn't work. The arrow just rotated towards the top center of my screen. Any ideas?
If the forward direction of your arrow is something else than +z you need to change the code accordingly. For example, if the arrow points in the +y direction, you need to use transform.up ins$$anonymous$$d of transform.forward.
Oh, I see. Well, I'm shooting an arrow, and I want the tip to point in the direction it's falling, so what if it's falling downward? Will it still work?
Answer by tylo · Sep 30, 2010 at 05:36 AM
This is how I do it.
void FixedUpdate(){
if(rigidbody.velocity != Vector3.zero)
rigidbody.rotation = Quaternion.LookRotation(rigidbody.velocity);
}
This one is actually my favorite. The movement seems like it belongs in FixedUpdate rather than Update.
Answer by StephanK · Apr 12, 2010 at 07:41 AM
Another way that should work is to adjust the arrows rigidbody.centerOfMass so that it is closer to the tip.
would that work? Both gravity and drag works on the center of mass, and does not provide any torque.
Answer by Ryder · Jun 06, 2010 at 11:02 PM
Actually, it's easy to do with one line of code. In this case, + .y is the direction of travel for the object (as if your arrow was modeled with the tip up). don't give the rigidbody any drag You just add a relative impulse to get it going... then
function FixedUpdate(){
// add a force like to drag, opposite the direction of travel, and apply it aft of the center of mass, typically (0,0,0) rigidbody.AddForceAtPosition(rigidbody.velocity * -.1,transform.TransformPoint(0,-.25,0));
}
If this creates too much drag, one can add a constant relative Force to the arrow to offset it
Your answer
Follow this Question
Related Questions
drag object including free rotation 0 Answers
How do drag and angular drag interact? 2 Answers
Clamp Bike Rotation 0 Answers
Rotating a Rigidbody with Physics 1 Answer
Simulating the graphics of a rolling 3D ball in a 2D game 2 Answers