How To Throw Objects



Hello everyone!

It's Josiah here to do another how-to on our game Not So Trash Panda. This post is sort of a part 2 to the previous how-to on picking up objects. These features function together so they use each other and reference each other directly.

Now let's get Started

The object throwing uses the object that you are holding though the object holding feature and applies math to throw it. We also added a line trajectory to show where the object will go, my colleague Shelby helped me with the line feature.

We start with the math needed to throw the objects.

This is the main code that contains the math needed to throw the objects. To throw the objects we use a rigid body and we use the AddForce method. AddForce needs a force vector and a force mode as parameters, we use the direction we want the object to go in through the finalThrowDirection and multiply that by the force of the throw. Force in this case is a parameter of the method Throw() that I created and which the code above is held. I used VelocityChange as the force mode because we are using physics to throw it, changing the velocity lets it freely slow down and speed up depending on how long it is in the air. The finalThrowDirection simply takes the forward direction of the player and then a Vector3 called throwDirection which just points directly up. We add them together so the final vector basically meets in the middle which would be around 45 degrees above the direction the player is facing. Then at the end we normalize it which basically rounds the Vector but maxes it to 1. 

For the throwing we use update to detect if the player can throw.


At the top we first check if the throw button is pressed and we make sure the player is actually holding an object. We then call our StartThrow() function.


All the StartThrow does is make sure that all the variables we use for charging the throw and the force are reset every time you try to throw. It also sets the isCharging bool to true which lets us call the ChargeThrow() method through update.


The Charge Throw method increases the chargeTime by real time and then decreases it in real time. We also use this to play an increasing and decreasing audio, this is also where we call our ShowTrajectory method that draws the path of the objects in the air while charging, that looks like this. 


We get 2 vector3’s as the parameters of that function, they are the origin and the speed. The origin is the start of the line and the speed is the speed of the object. In charge throw we set the origin to be between the center of the player and where the player holds objects which comes out to be around the players face. And then the speed is simply the velocity of the object being thrown. How we portray the line is by making an array of vector points that follow the trajectory of the object and then we set a line renderer to the points in the array basically making an arc of the throw. 

Once the Throw button is released we call the ReleaseThrow method.


There is a lot here but the main thing this does is call the Throw function and provide it with the proper data for the parameters. The other things are just to make sure the object is considered dropped and that proper sounds are played. 

The actual throw method contains the math we talked about at the top plus many other things. 


The most important chunks of code lay at the bottom, having the AddForce we talked about before as well as resetting the chargeTime back to zero we can reset the throw for next time. The upper portions are for things like spawning particles for task completion which I won’t get into here, In the middle we make sure to get a reference to the held object's rigid body through rb and we also make sure the object is technically dropped in the ObjectHolding scripts eyes. If we don’t drop it the object will just go back to where the object holds it. So to get past that we have two different game objects, heldObj and heldObject. heldObject is the object being held from the ObjectHolding script and heldObj is created in the ObjectHolding script. They are set to the same game object once you pick it up but we can influence them independently. So we make sure the heldObject is dropped and then we throw the heldObj, if we didn’t do this then the code would have no way to influence the game object once dropped. 

And after all that this is the final product

Thank you for reading and I hope you learned something, also play our game please!

Get Not so Trash Panda

Leave a comment

Log in with itch.io to leave a comment.