Setting up Character Movement

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

Step 1

Creating the GameObjects

  • Create a player character  (the cube)
  • Create a ground (a plane)

Screen Shot 2017-02-14 at 10.20.30.png

Step 2

Adding player things to the player

  • Rigidbody

screen-shot-2017-02-14-at-10-22-30

 

Step 3

Adding a playerMovement Script

Screen Shot 2017-02-14 at 10.23.30.png

 

Step 4

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.

screen-shot-2017-02-14-at-10-26-27

Step 5

The sprint…

  • 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.

Screen Shot 2017-02-14 at 10.26.58.png

Step 6

The Jump…

  • 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…

Screen Shot 2017-02-14 at 10.32.00.png

Step 7

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
    • OnTriggerEnter
    • OnTriggerExit
  • 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

Screen Shot 2017-02-14 at 10.35.06.png

Step 8

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

Screen Shot 2017-02-14 at 10.42.05.png

Step 9

Attach your scripts to the player

  • AddComponent to the Player
  • Add Scripts
  • Choose to add the scripts you just wrote
    • Player Jump
    • Player Movement

 

Step 10

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.

Screen Shot 2017-02-14 at 10.43.52.png

Step 11

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.

Screen Shot 2017-02-14 at 11.02.44.png

Step 12

This is what my unity looks like at the end of doing these steps…

screen-shot-2017-02-14-at-10-17-47

 

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

Step 1

You can download it from here: https://github.com/360Controller/360Controller/releases.

Step 2

To check that it is all working go to System Preferences and it should appear down the bottom.

screen-shot-2017-02-14-at-10-52-01

By clicking on it you are given a window where you can see if the computer is receiving inputs from the controller.

screen-shot-2017-02-14-at-10-52-10

 

Step 3

Go to Unity and Edit -> Project Settings -> Input to bring up the Input manager.

screen-shot-2017-02-14-at-10-54-03

The Input Manager is where you can change the Inputs, for example to use the joystick controls rather than the keyboard ones.

screen-shot-2017-02-14-at-10-54-17

Step 4

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

 

Step 5

I want to use a joyStick button for the run and jump.

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s