- Home /
Throwing a Spear
Hi,
I have been trying to find a way of getting a spear throw to correctly 'dip' at the head and fly correctly. I have found that using the following code should work:
public void ReleaseSpear () {
rigidB.useGravity = true;
rigidB.AddForce (gameObject.transform.forward =
Vector3.Slerp(gameObject.transform.forward, rigidB.velocity.normalized, Time.deltaTime * 5)* 18000);
gameObject.transform.parent = null;
}
Basically all this does is turn gravity on, adds force to the gameobject and unparents it from the hand.
The spear model has the blue (forward) axis pointed forwards and yet it still flys tail 'heavy' and lands in the ground tail down?
Can anybody see why this is happening?
An image of the spear once launched? Notice the tail dipping and not the head?
Wow nice game (although might have a smaller target audience)!
Answer by sparkzbarca · Dec 30, 2017 at 06:26 PM
have you tried making the tip of the spear and the shaft of the spear separate objects, make the spear head the parent, the shaft the child and increase the mass of the spear tip? This is in fact of course why spears actually fly the way they do physically and you are trying to emulate that with a physics engine.
If you dont want to modify the model you can just create an empty gameobject with a lot of mass and put it in the center of the spear tip and parent to that
@sparkzbarca - Thanks for the reply - I have tried this (and again to see if I did something wrong) but again it just behaves in the same manner.
I have also tried to to set the Quaternion Rotation to the rigid body velocity (to make the spear point towards the destination vector over time) but again this does not seem to work (weird behaviour were the spear travels sideways and then moves inwards over time). I thought this was working and perhaps the axis were off but after trying multiple options it still behaved the same.
I have also explored trying to offset the centre of mass of the gameobject but again this did not work.
Ste.
void FixedUpdate(){ if(rigidbody.velocity != Vector3.zero) rigidbody.rotation = Quaternion.LookRotation(rigidbody.velocity);
}
credit:
https://answers.unity.com/questions/14899/realistic-rotation-of-flying-arrow.html
The general consensus seems to be the key is both mass in the tip and DRAG in the back. You can physics that with empty game objects with larger drag co-efficents but most people think that's really overcomplicating it and your better to code simluate it than actually go full on physics.
You sir are a legend.
Thank you - I couldn't understand why the code was not working as it all looks fine. I had even attempted the Quaternion.LookRotation from that thread but didn't put it into a fixed update.
So the now the spear flies perfectly. The code was this:
public void ReleaseSpear () {
gameObject.transform.parent = null;
rigidB.useGravity = true;
rigidB.AddForce (gameObject.transform.forward * 30000);
Vector3.Slerp(gameObject.transform.forward,rigidB.velocity.normalized, Time.deltaTime * 2);
rigidB.ResetCenterOf$$anonymous$$ass();
}
void FixedUpdate () {
if (rigidB.velocity != Vector3.zero) {
rigidB.rotation = Quaternion.LookRotation (rigidB.velocity.normalized);
}
}
Your answer
Follow this Question
Related Questions
Fireing a tank projectile. 1 Answer
[Physics] Compensating for Moving/Rotating Terrain 0 Answers
Multiple Cars not working 1 Answer
How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers