- Home /
Projectile motion of an arrow after applying force
I have been banging my head against the wall for days, trying to solve this problem but I am not getting any solution. I am trying to achieve a real projectile motion of an arrow after applying a force to the arrow of certain magnitude in a particular direction. Actually I want the arrow to follow a projectile motion with the head of the arrow going upward for first half of the projectile motion & the head of the arrow facing downward for the second half of the projectile motion. For clear vision see the attached video below:
https://www.youtube.com/watch?v=VwHcqheekt8
I have taken a sprite with the Sprite Renderer of an arrow & a Rigidbody 2D attached to it. The mass of the RIgidbody 2D attached to the arrow sprite has a mass of 0.0001, Linear & Angular drag of 0.05 , gravity scale of 0. There are two empty objects,ArrowHead & ArrowTail, inserted inside this arrow sprite. Bothe these empty objects are having RigidBody 2D attached to these. ArrowHead object has got a Mass of 3 while ArrowTail object has got a mass of 1. After button click the a force is applied to the arrow sprite in (ArrowHead - ArrowTail) direction. But the empty objects attached to the arrow sprite do not move synchronously with the arrow after applying force.
Any help regarding this will be highly appreciated.
Answer by senad · Apr 01, 2014 at 10:22 AM
This might not help you directly with your problem, but have you considered solving it without the physics engine?
As you can see, it takes quite some fiddling to get it right. Computing the trajectory and rotation for an arrow can be done in the code without the physics engine.
Unless you have some compelling reason to use physics, the other aproach might be easier to implement, change and debug.
Thanks for the reply ...
But can you guide me through this without using the physics ... It will be very helpful ...
From the starting point you calculate a trajectory like described here: http://en.wikipedia.org/wiki/Trajectory
Then you get a function f(t). With this function you can calculate the position at a given time t. Every frame now you calculate the position of the arrow and move it to the position from the trajectory-function.
I would advise you to read this stuff up. You will then have full understanding und control over your code and the behaviour of the arrow, trajectory, etc.
:)
Answer by jamesflowerdew · Apr 01, 2014 at 10:36 AM
As above I'd be tempted to apply gravity without the physics
pseudocode below:
pArrowMomentum+=new Vector(0,MyGravity*Time.deltaTime,0);
transform.LookAt(transform.position+pArrowMomentum);
transform.position+=pArrowMomentum*Time.deltaTime;
you could apply the above to rigidbody.velocity if you're desperate to use physics but with fast objects like arrows I'd be really tempted to use raycasts, as arrows/bullets can pass through physics objects if their speed is high enough.
Answer by robertbu · Apr 01, 2014 at 03:44 PM
For most arrow situations, you want the arrow to follow the velocity. Assuming 2D sprite in which the front of the arrow points right when the rotation is (0,0,0), you can do:
var dir = rigidbody2D.velocity;
if (dir != Vector3.zero) {
angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
@robertbu Thanks for the help. It really worked like a charm. I really appreciate your help...
Your answer
Follow this Question
Related Questions
Projectile(Arrow) angle during flight is not realistic 1 Answer
Questions about a Bow and Arrow FPS setup 1 Answer
A node in a childnode? 1 Answer
Calculate Height of Trajectory : Unity3d 1 Answer
Vector3 and power 1 Answer