Prototyping a Unity Project: A simple scene with interactions
As we looked at how to create a Unity Project in our previous article, we will setup a simple scene to demonstrate motion and interactions.
Objective: Set up our GameObject's with components to allow for interactions and movement.
We can setup our GameObject’s Floor and Player Character with appropriate component's.
We replace the default Mesh Collider with a primitive Box Collider, it’s cheaper for performance cost’s than using a Mesh Collider for a simple shape.
Next we will add a Rigidbody component to our Player Character in the Inspector. This allows us to enable use of the PhysX Engine.
Having our Rigidbody attached to our Player Character can also affect the children component’s inside the parent object.
Enabling Gravity on our Rigidbody will add gravity and we can toggle it on and off as desired.
We can make our Player Character move with a Rigidbody component attached with forces and slopes. Additionally, we can move within Script’s.
We will make a new script called PlayerController.
Our PlayerController Class is responsible for Player Character information to store for our game logic.
Now that we have access to our Rigidbody or ‘rB’, we can Null Check to see if it has been destroyed or missing so we don’t run into frequent error’s in the future.
We can simply return the condition if the Rigidbody is not null.
Moving our Player Character in Update.
The Console will output if we have held down the ‘W’ Key (GetKey) / (GetKeyDown) is a single press.
Applying Movement with our Rigidbody.
Currently we have a basic implementation to apply movement with our Rigidbody, and we need to make a speed amount for it to work properly.
We can adjust our speed variable in the Inspector.
So we have Rigidbody movement now in the forward direction when the W-key is held down.
Challenge: Complete the Input values to reflect the final result below.
Final result of the Rigidbody ‘rB’ movement.