Throwing an object whilst moving
I have a gameobject (sphere) which can be thrown using the left mouse button. It gets thrown in the rotation I look at (the sphere is a child of the head of the player, so that it moves with him). The problem is, if I throw the sphere while moving, it lands behind the player, because the moving speed of the player is not added to it.
I move my player like this:
float forward = Input.GetAxis("Vertical");
float right = Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * forward * forwardSpeed * Time.deltaTime +
Vector3.right * right * rightSpeed * Time.deltaTime);
I tried this with applying force to a rigidbody, but then the character is gliding over the ground (and the result was still the same).
I throw the ball like this:
body.useGravity = true;
body.isKinematic = false;
body.AddForce(transform.forward * throwForce, ForceMode.Impulse);
The Hierarchy is like follows: Player -> Head -> Thrower -> Ball
So how can I do a physically "correct" throw?
Why not pick up the forwardSpeed value from the player movement script and multiply by/add to throwForce?
You might have to play with the figures to see what works but you only really need the forward motion if only ever throwing forward.
body.AddForce(transform.forward * throwForce * forwardSpeed, Force$$anonymous$$ode.Impulse);
EDIT although if that's an axis you might want
throwForce + (forwardSpedd * 3) or similar but play with it and see what works.
Answer by Senhyro · Nov 17, 2017 at 12:55 AM
Is the throweable object collidable with the player? Looks like it collides with the player when it's thrown. So it falls behind the player.
-EDIT-
Even though it's Kinematic, just make sure you don't have any script going on so it collides with the player!
Your answer
![](https://koobas.hobune.stream/wayback/20220612082352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody bouncing off other rigidbodies 0 Answers
Jumping cancels out x and z velocity. 0 Answers
Knockback with limited movement 0 Answers
How to fix this problem? 1 Answer