INFINITY SERIES

SCRIPTS & TUTORIALS
First Person RPG | Player Movement
This package expands on the first and adds a "Player Object" with simple movement controls. The RPG I'm looking to build doesn't required physics for the movement, so we use a Character Controller. Keyboard/Mouse movment/looking in a lightweight package. We also add the Game Controller object with gameControl.js script to manage whether the game is paused or not (different from the time-keeping pause from the last tutorial).

Download Unity Package

Video Tutorial


Script(s)
gameControl.js
#pragma strict

static var paused		: int		= 0;	// Is the game paused? (IE, no player/enemy/particle/gravity movement etc) 0 = not paused

// Call this to add or remove a pause value.  If paused = 0, then game isn't paused.  Other things can add to it (multiple windows etc)
// and remove it when they're closed, so that only when it's all closed cand the game be un-paused.
//
// [For my own game, I'll be using this value when the player is navigating the menus.  That way they can't move, 
// and in-game things like animations, particles etc can be paused as well.  This is different from the in-game timer
// stopping, as that can also happen while the player is able to look around and particles will animate etc, during
// turn-based combat]
function AdjustPause(newValue : int){
	paused += newValue;
}

----------------------------------------------------------
YOU ARE FREE TO USE THESE SCRIPTS IN YOUR PROJECT IN ANY WAY YOU WOULD LIKE!
IF YOU HAVE PURCHASED OUR MODELS TO USE IN YOUR PROJECTS, WE THANK YOU VERY
MUCH FOR YOUR SUPPORT!  WITHOUT IT, WE COULDN'T KEEP MAKING MORE KILLER
ASSETS FOR YOUR GAMES.

http://www.InfinityPBR.com
----------------------------------------------------------
playerMovement.js
#pragma strict

var moveSpeed			: float			= 3.0;				// Movement speed
var moveSpeedMod		: float			= 1.0;				// Modifier for Movement Speed (used for "slow" spells etc)
var rotateSpeed			: float			= 240.0;			// Speed for horizontal rotation
var tiltSensitivity		: float			= 3.0;				// Sensitivity (multiplier) for vertical rotation
var invertVertical		: boolean		= false;			// Invert the vertical rotation?
var maxTilt			: float			= 75.0;				// Maximum tilt for camera
var gravityMultiplier		: float			= 2.0;				// Multiplier for falling
var playerCamera		: GameObject;						// Camera for player
var characterController		: CharacterController;					// Link to this component

function Start () {
	characterController		= GetComponent(CharacterController);		// Cache the component
	if (PlayerPrefs.GetInt("Invert Vertical"))
		invertVertical		= true;
	else
		invertVertical		= false;
}

function Update () {
	if (gameControl.paused == 0)
	{
		DoMovement();
		DoRotation();
	}
}

function DoMovement(){
	var moveDirection	: Vector3;
	// Compute the movement
	var horMove		= Input.GetAxis("Horizontal") * moveSpeed * moveSpeedMod;
	var verMove		= Input.GetAxis("Vertical") * moveSpeed * moveSpeedMod;
	moveDirection 		= Vector3(horMove, Physics.gravity.y * gravityMultiplier,verMove);
	moveDirection 		= transform.TransformDirection(moveDirection);
	characterController.Move(moveDirection * Time.deltaTime);					// Move the character with the controller
}

function DoRotation(){
	var sideRotation		= Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime;	// Compute sideways rotation
	transform.eulerAngles.y		+= sideRotation;						// Perform sideways

	var verticalValue		= Input.GetAxis("Mouse Y") * tiltSensitivity;			// Save mouse Y value
	if (!invertVertical)										// If player wants to invert the tilt
		verticalValue		*= -1;								// Multiply the value by -1
	var cameraRotation		= playerCamera.transform.localRotation;				// Grab the current rotation
	// The following lines, with the function they call, are taken from another script provided by Unity.
	cameraRotation 				*= Quaternion.Euler(verticalValue, 0, 0);
	cameraRotation 				= ClampRotation(cameraRotation);
	playerCamera.transform.localRotation	= cameraRotation;
}

// This function was translated from the MouseLook.cs Standard Asset script.
function ClampRotation(q : Quaternion) : Quaternion{
    q.x /= q.w;
    q.y /= q.w;
    q.z /= q.w;
    q.w = 1.0;

    var angleX	: float		= 2.0 * Mathf.Rad2Deg * Mathf.Atan(q.x);
    angleX			= Mathf.Clamp(angleX,-maxTilt, maxTilt);
    q.x 			= Mathf.Tan(0.5 * Mathf.Deg2Rad * angleX);

    return q;
}

----------------------------------------------------------
YOU ARE FREE TO USE THESE SCRIPTS IN YOUR PROJECT IN ANY WAY YOU WOULD LIKE!
IF YOU HAVE PURCHASED OUR MODELS TO USE IN YOUR PROJECTS, WE THANK YOU VERY
MUCH FOR YOUR SUPPORT!  WITHOUT IT, WE COULDN'T KEEP MAKING MORE KILLER
ASSETS FOR YOUR GAMES.

http://www.InfinityPBR.com
----------------------------------------------------------