So what to do next? Well, better start making some of the game mechanics. I think the most basic one to start with is character movement, so lets start with that one!
Check out the Screen Recording of this Process
Character Movement in Unity
Creating the GameObjects
- Create a player character (the cube)
- Create a ground (a plane)
Adding player things to the player
Adding a playerMovement Script
The code for the player movement script. Here I’m going to be moving the player using forces. The walkSpeed is a public value so I can quickly change it later in Unity.
- I add in another public variable called runSpeed.
- I then add in a check to see if a button is being pressed (I shall show you how to do this with the xBox controller config bit).
- There is also an if statement to check which speed to use – depending on which button is being pressed.
- Is a new c# script
- It has a public variable jumpVelocity
- It checks if a button is being pressed
- Creates a new Vector 3
- And with an if statement checks if that jump button is being pressed to AddForce to the player.
The only problem with this script is that the player does not land…
Adding a limit to the jump – so it looks like a jump rather than a jet pack.
- I have added a public variable grounded which checks if the player is ‘on the ground’ or not. This is public for debugging purposes as Unity shows you a tick box, indicating true/false.
- My if statement now checks 2 things:
- If the jumpButton is down
- If the player is grounded.
- To change the state of grounded I have added in 2 more functions
- In these functions we check if the player (other) has collided with a GameObject with the Tag: Ground
- If the player is touching ground – grounded = true
- If the player is not touching ground – grounded = false
Adding the right information to Ground
- Back in Unity click on your Ground and look at the inspector.
- Ground needs the Tag: Ground (I added a new tag for this)
- The Box collider on the Ground needs to be a trigger
Attach your scripts to the player
- AddComponent to the Player
- Add Scripts
- Choose to add the scripts you just wrote
- Player Jump
- Player Movement
Checking the right information is on the Player
- The collider is currently not a trigger
- The Rigidbody uses gravity
- You should now see your scripts at the bottom as well
- You can now add in some values for: Walk Speed, Run Speed and Jump Velocity.
At the moment I don’t want my player to rotate everywhere so I added in a FreezeRotation into the constraints. But I will later change this so that the right joystick pad controls the rotation of the player.
This is what my unity looks like at the end of doing these steps…
Using an Xbox Controller for Movement
I am developing on a mac and there are no offical drivers for xBox 360 controllers but this one is very good: https://github.com/360Controller
You can download it from here: https://github.com/360Controller/360Controller/releases.
To check that it is all working go to System Preferences and it should appear down the bottom.
By clicking on it you are given a window where you can see if the computer is receiving inputs from the controller.
Go to Unity and Edit -> Project Settings -> Input to bring up the Input manager.
The Input Manager is where you can change the Inputs, for example to use the joystick controls rather than the keyboard ones.
I want to change the Horizontal and Vertical controls to be on the left joypad.
- So I select Type at the bottom and choose Joystick Axis
- Axis X or Axis Y depending on if it is the horizontal / vertical input
I want to use a joyStick button for the run and jump.
- I add in the text joystick button and then the number I want to use
- To find the number I wanted to use I used I referenced it here: http://wiki.unity3d.com/index.php?title=Xbox360Controller
- Different drivers and operating systems have different numbers for the buttons.