You are on page 1of 2

using UnityEngine;

public class ThirdPersonUserControl : MonoBehaviour {
public bool walkByDefault = false; // toggle for walkin
g state
public bool lookInCameraDirection = true; // should the charac
ter be looking in the same direction that the camera is facing
private Vector3 lookPos; // The position that
the character should be looking towards
private ThirdPersonCharacter character; // A reference to the Th
irdPersonCharacter on the object
private Transform cam; // A reference to th
e main camera in the scenes transform
private Vector3 camForward;
// The current forward direction of the camera
private Vector3 move;
// the world-relative desired move direction, calculated from the camForward and
user input.
// Use this for initialization
void Start ()
// get the transform of the main camera
if (Camera.main != null)
cam = Camera.main.transform;
} else {
Debug.LogWarning("Warning: no main camera found. Third p
erson character needs a Camera tagged \"MainCamera\", for camera-relative contro
// we use self-relative controls in this case, which pro
bably isn't what the user wants, but hey, we warned them!
// get the third person character ( this should never be null due to req
uire component )
character = GetComponent<ThirdPersonCharacter>();
// Fixed update is called in sync with physics
void FixedUpdate ()
// read inputs
bool crouch = Input.GetKey(KeyCode.C);
bool jump = CrossPlatformInput.GetButton("Jump");
float h = CrossPlatformInput.GetAxis("Horizontal");
float v = CrossPlatformInput.GetAxis("Vertical");
bool jump = Input.GetButton("Jump");
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
// calculate move direction to pass to character
if (cam != null)
// calculate camera relative direction to move:
camForward = Vector3.Scale (cam.forward, new Vector3(1,0
move = v * camForward + h * cam.right;
} else {
// we use world-relative directions in the case of no ma
in camera
move = v * Vector3.forward + h * Vector3.right;
if (move.magnitude > 1) move.Normalize();
// On non-mobile builds, walk/run speed is modified by a key pre
bool walkToggle = Input.GetKey(KeyCode.LeftShift);
// We select appropriate speed based on whether we're walking by
default, and whether the walk/run toggle button is pressed:
float walkMultiplier = (walkByDefault ? walkToggle ? 1 : 0.5f :
walkToggle ? 0.5f : 1);
move *= walkMultiplier;
// On mobile, walk/run speed is controlled in analogue fashion b
y the v input value, and therefore needs no special handling.
// *hence no code here!*
// calculate the head look target position
lookPos = lookInCameraDirection && cam != null
? transform.position + cam.forward * 100
: transform.position + transform.forward * 100;
// pass all parameters to the character control script
character.Move( move, crouch, jump, lookPos );