Hello and welcome to this update post. I hope you all had a wonderful break over the holidays. Today I will be presenting some news about what I have been working on. As the title says, today will be about some updates I have made to my interaction system. Lets get started!
Interaction System
I have been busy delving into the interaction system which will allow the player to essentially interact with the game world around them. Now that the fundamentals of the system were in place I needed to break down the types of interactions I wanted the player to be capable of. From what I had discovered, there were 2 main categories that each interaction could be placed in; Object interactions and World Interactions.
If we take a player in a basic bedroom scene, What I mean by these categories is this:
An object interaction would be them being able to pick up an object off the desk (a key or phone for example).
A world interaction would them being able to use something in the scene this isn't portable, or is a static fixture for the scene (a light switch or door for example).
The interactions that I had created in the previous post only really fit into the world interaction category so I decided to try and implement something that allowed the player to pick up an item and "hold" it in front of them.
Object Interaction
The first problem that I identified was that in all of the interact scripts for items, the item to be interacted with was a toggle of some kind; Is the light source active or not, is the audio source playing from the radio muted or not. What this meant was that trying to interact with the entire object needed additional code to incorporate the object interaction with the current system. The system worked well for some simple actions but didn't include a suitable, out-of-the-box solution to the interaction of being able to pick up and throw items.
Pick Up and Throw
Unsure as to how exactly I wanted to implement the pick up and throw interaction, I started with looking into which method of item interaction I wanted to create. That may sound weird, but let me explain:
To me there are two different ways games mainly implement a physical item pick up feature.
Have the object lock to an "anchor" point in front of the player.
Have the item's gravity ignored and locks its position in reference to the player.
Now, from what I primarily designed, using these two different methods as examples, is that when the feature will need to incorporate the player's avatar as the focal point regardless of the method. The only question was HOW does the player avatar work as the focal point.
As I am a very practical learner, and learn better by doing things and solving issues, I started straight away by creating a separate script that would handle this interaction.
Firstly, as the object interacted with needed to be able to be thrown and dropped, I decided to use a rigidbody for its physics, as this already calculates gravity and enacts it onto the object. Huzzah, the ability for the object to fall was implemented (not very hard but still). The next task to tackle was the ability to pick up the object so I created an anchor point onto the camera attached to the player model we have set up in our project. What this is, is a very simple empty game object that consists of a Transform component and nothing else.
Once I had added this anchor to the player model, I created a reference to it in the script I had made. For now this is assigned via Unity's inspector window, but will need to be assigned dynamically in the future. I had to also add a reference to the rigidbody of the object that I was to interact with, this was a simple assignment via script due to the component being on the same object as the script.
After the rigidbody and anchor points were assigned, I started on the main part of the script; the interact method. As I was making an interactable object, I made it so the script inherited from the IInteractable interface so it would link into the interaction system properly. This also gave me access to the inherited Interact() method needed.
The premise of the interact function was simple, the object needed to be able to be picked up if it weren't already held, or dropped/thrown if it was. This was a simple Boolean check that was changed based on the 'state' of the object. Once the state was set, certain actions needed to happen. The way that I have implemented the pick up simply sets certain properties of the objects rigidbody, parentage, and Transform.
To drop/throw the object, it simply removes the parent of the object, adds a force on the object in the direct away from the player, then resets the rigidbody's use of gravity and rotation lock to default (that being, enable gravity and unfreeze the rotation). Clearly adding force in the direction the player is facing doesn't drop the object, this is because my understanding of the input system is basic and I haven't been able to add in the detection of a held button yet. Once I have done that I can use that to determine whether the player wants to drop or throw the object based on input duration; Throwing the object when the button is held and then released or dropping it on input tap (depicted in the code comments).
That is it for this post. I haven't managed to implement the second method for player object interaction just yet but hopefully that is something that can be talked about in the next post.
As always, if you would like to stay up to date, be sure to check back regularly to see if there is any news or updates regarding this project!
Comments