codesamples >> projectdemon playercontroller class

Project Demon: PlayerController

Project:

ProjectDemon

Engine:

Unity

Code language:

C#

Class description:

The playercontroller class handles the actions of the main player. I made the controls feel like the topdown actiongame called 'Hotline Miami'.

PlayerController.cs

using UnityEngine;
using System.Collections;

// The various states of the Player
public enum PlayerStates
{
	Alive,
	Dead
}

// The class which controls and manages all the player's input and actions
public class PlayerController : MonoBehaviour 
{
	// The overall gravity
	public static float GRAVITY = 9.81f;
	
	// Is the player currently attacking
	public bool IsAttacking = false;
	
	// The movement speed of the player
	public float MovementSpeed = 15.0f; 
	
	// The distance of the camera
	public float CameraDistance = 2.0f;
	
	// The height of the camera
	public float CameraHeight = 20.0f;
	
	// The time the attack of the player remains active
	public float AttackTime = 0.25f;
	
	// The max time an attack of the player will remain active
	private float _maxAttackTime = 0.0f;

	// Use this for initialization
	void Start () 
	{
		// Make the initial attacktime the maximum
		_maxAttackTime = AttackTime;
	}
	
	// Update is called once per frame
	void Update () 
	{
		LookAtMousePosition();
		MoveCharacter();
		UpdateCamera();
		
		// (Debug) Draw the Ray between Character and Mouse position
		Debug.DrawRay(transform.position, GetMousePosition()-transform.position, Color.green);
		
		// Make the player attack
		if(Input.GetButtonDown("Fire1"))
		{
			Debug.Log("Melee attack start");
			IsAttacking = true;
			AttackTime = _maxAttackTime;
		}
	
		// Reduce the time the attack of the player remains active
		AttackTime -= Time.deltaTime;
		if(AttackTime < 0 && IsAttacking)
		{
			Debug.Log("Melee attack stopped");
			IsAttacking = false;
		}
	}
	
	// Makes the Character LookAt the position of the Mouse in WorldSpace
	private void LookAtMousePosition()
	{
		Vector3 lookTarget = GetMousePosition();

		// Make the lookat point the same height as the character
		lookTarget = new Vector3(lookTarget.x, transform.position.y, lookTarget.z);
		
		// Lookat the MouseCursor
		transform.LookAt(lookTarget, new Vector3(0, 1, 0));
	}
	
	// Returns the position of the Mouse in WorldSpace
	private Vector3 GetMousePosition()
	{
		// Cast a ray to see where the MouseCursor is in WorldSpace
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit = new RaycastHit();
		
		if (Physics.Raycast (ray, out hit)) 
			return hit.point;
		else
			return Vector3.zero;
	}
	
	// Makes the Character move based on the input of the Player
	private void MoveCharacter()
	{
		CharacterController controller = GetComponent<CharacterController>();
		Vector3 direction = new Vector3(Input.GetAxis("Horizontal"),-GRAVITY,Input.GetAxis("Vertical"));
		Vector3 movementOffset = direction * MovementSpeed * Time.deltaTime;
		controller.Move(movementOffset);
	}
	
	// Positions the camera on the correct spot
	private void UpdateCamera()
	{
		// Calculate the Camera Position
		Vector3 viewVector = GetMousePosition() - transform.position;
		viewVector.y = 0;
		viewVector.Normalize();
		viewVector *= CameraDistance;
		
		// The height of the Camera
		viewVector.y = CameraHeight;
		
		Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, 
		transform.position + viewVector, 0.1f);
	}
}

MeleeTrigger.cs

using UnityEngine;
using System.Collections;

public class MeleeTrigger : MonoBehaviour 
{

	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	
	void OnTriggerStay(Collider other) 
	{
		if (other.gameObject.name == "Enemy" && 
		GameObject.Find("Player").GetComponent<PlayerController>().IsAttacking)
		{	
			Debug.Log(other.gameObject.name);
			Destroy(other.gameObject);
		}
    }
}