Game Development Reference
In-Depth Information
Gotta move it!
Our player now has a basic idle animation, but we can't interact with him yet. Let's
fix that. Here is where we add the ability for the player to move around the scene.
We are going to use the existing key bindings that are present by default in a Unity
project. To see what these are or change them, navigate to
Edit
|
Project Settings
|
Input
and mess around. Unity stores all keybinds as axes, as they all have floating
point values. This allows all input buttons for the engine to support the classic
on/off function as well as support more touch-sensitive buttons and joysticks,
such as those present on most modern gamepads.
Let's create a new folder in our project folder called
Scripts
, and inside that, create a
new C# script. Call this script
PlayerStateController
. Here's how it should look:
using UnityEngine;
using System.Collections;
public class PlayerStateController : MonoBehaviour
{
public enum playerStates
{
idle = 0,
left,
right,
jump,
landing,
falling,
kill,
resurrect
}
public delegate void playerStateHandler(PlayerStateController.
playerStatesnewState);
public static event playerStateHandleronStateChange;
void LateUpdate ()
{
// Detect the current input of the Horizontal axis, then
// broadcast a state update for the player as needed.
// Do this on each frame to make sure the state is always
// set properly based on the current user input.
float horizontal = Input.GetAxis("Horizontal");
if(horizontal != 0f)
{