Sunday, August 10, 2025

HORSE Tutorial: Grab

    In this tutorial, we'll go over the script that controls the grab and throw mechanics for the paper balls in the game Trash HORSE, which can be played here, or the full game package can be downloaded here.

    This portion of the script controls whether or not the player can pick up the ball. The Distance block checks the distance between the paper ball (this) and the player (player) which is a scene variable. Once we have that distance, we use the If block to check if the distance between the player and the paper ball is less than or equal to 4 meters. So, basically checking if the player should be able to reach the ball or not, so they aren't grabbing a paper ball from across the room.

    Also in this script is the On Mouse Down block. This is to say when the player has their mouse hovering above an object and they clock the Left Mouse Button, the script will fire. The object in question needs to have a collider for this to work.

 


    New stop, we have a check to see if the player already has something in their hands. So, we have a boolean variable called Holding which is a scene variable so lots of different scripts attached to lots of different objects can check and change it. We check if Holding = false, meaning the players hands are empty, and if they are empty we move on and set the variable to true, so the players hands are now full. And now even if there is another paper ball within 4 meters, the player can't grab it.


     Next, we move the paper ball into the players "hands" by making the paper ball a child of an empty object we have attached to the First Person Controller just under the camera, or where we usually see a gun held in a generic FPS. Then we set the Local Position to 0,0,0 meaning the paper ball will be at exactly the same place as the Hold_Point and change the rotation to match.


     Next we change the Object specific variable of Held to equal true. So, the paper ball is now in position and it registers itself as being held. Then we move to the Rigidbody block, Set Constraints, and freeze all, which will stop the paper ball from moving away from the players hands. After that, we turn the collider off (Set Enabled = False) so the ball doesn't smash into the players own collider and create weird problems like the player standing on what is in their own hands.

This took so much longer than I expected, so we're going to stop here and go over the next part of the script, throwing, in the next tutorial. Again, you can test out how all of this works in the web version here on Itch.io, or if you want to dive into the scripts yourself, you can download the whole package here for Unity.

Later.


 

 

No comments:

Post a Comment