Created
October 20, 2014 18:27
-
-
Save dyadica/6ac49240eebd960ce962 to your computer and use it in GitHub Desktop.
A Unity MonoBehaviour that can be applied to a GameObject to provide simple turn on the spot joystick style player movement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[RequireComponent (typeof(CharacterController))] | |
public class PlayerController : MonoBehaviour | |
{ | |
/// <summary> | |
/// The players character controller | |
/// </summary> | |
private CharacterController controller; | |
/// <summary> | |
/// The desired rotation of the player | |
/// </summary> | |
private Quaternion targetRotation; | |
/// <summary> | |
/// A float value used to set the speed of smoothed rotation | |
/// </summary> | |
public float RotationSpeed = 400f; | |
/// <summary> | |
/// A float value used to set the players walking speed. | |
/// </summary> | |
public float WalkSpeed = 2; | |
/// <summary> | |
/// A float value used to set the players running speed. | |
/// </summary> | |
public float RunSpeed = 6; | |
void Start () | |
{ | |
// Set the reference to the character controller | |
controller = GetComponent<CharacterController>(); | |
} | |
void Update () | |
{ | |
// Get the input for both the horizontal and vertical axis. Use raw | |
// to avoid acceleration. | |
Vector3 input = | |
new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); | |
// If input is not equal to zero tell the character to look at the | |
// input direction. | |
if (input != Vector3.zero) | |
{ | |
// Set the input direction | |
targetRotation = Quaternion.LookRotation(input); | |
// Smooth the rotation by multiplying vector Y by the | |
// smoothed targetRotation Y. Smoothing is achieved | |
// via the MoveTowardsAngle function. This in turn is | |
// customisable using RotationSpeed | |
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle( | |
transform.eulerAngles.y, | |
targetRotation.eulerAngles.y, | |
RotationSpeed * Time.deltaTime | |
); | |
} | |
// Create a temporary property to be used to calculate motion. | |
// Initialy set motion to equal our input value. | |
Vector3 motion = input; | |
// Cater for angular velocity | |
motion *= (Mathf.Abs(input.x) == 1 && (Mathf.Abs(input.z) == 1)) ? .7f : 1f; | |
// Cater for run or walk. Multiply input dependant upon state. | |
motion *= (Input.GetButton("Run")) ? RunSpeed : WalkSpeed; | |
// Apply gravity | |
motion += Vector3.up * -9.81f; | |
// Move the controller | |
controller.Move(motion * Time.deltaTime); | |
} | |
} | |
// <copyright file="PlayerController.cs" company="dyadica.co.uk"> | |
// Copyright (c) 2010, 2014 All Right Reserved, http://www.dyadica.co.uk | |
// This source is subject to the dyadica.co.uk Permissive License. | |
// Please see the http://www.dyadica.co.uk/permissive-license file for more information. | |
// All other rights reserved. | |
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY | |
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A | |
// PARTICULAR PURPOSE. | |
// </copyright> | |
// <author>SJB</author> | |
// <email>contact via facebook.com/adropinthedigitalocean</email> | |
// <date>20.10.2014</date> | |
// <summary>A Unity MonoBehaviour that can be applied to a GameObject to provide | |
// simple turn on the spot joystick style player movement. | |
// </summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment