0% found this document useful (0 votes)
5 views

PlayerController

The document contains a C# script for a PlayerController in Unity, which manages player movement and camera control. It includes features such as adjustable yaw and pitch for camera rotation, movement speed settings, and the ability to sprint. The script also handles player input for movement and toggles control visibility based on user interaction.

Uploaded by

Mykola Podkaluk
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

PlayerController

The document contains a C# script for a PlayerController in Unity, which manages player movement and camera control. It includes features such as adjustable yaw and pitch for camera rotation, movement speed settings, and the ability to sprint. The script also handles player input for movement and toggles control visibility based on user interaction.

Uploaded by

Mykola Podkaluk
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
public float MinYaw = -360;
public float MaxYaw = 360;
public float MinPitch = -60;
public float MaxPitch = 60;
public float LookSensitivity = 1;

public float MoveSpeed = 10;


public float SprintSpeed = 30;
private float currMoveSpeed = 0;

protected CharacterController movementController;


protected Camera playerCamera;

protected bool isControlling;


protected float yaw;
protected float pitch;

protected Vector3 velocity;

protected virtual void Start() {

movementController = GetComponent<CharacterController>(); //
Character Controller
playerCamera = GetComponentInChildren<Camera>(); // Player
Camera

isControlling = true;
ToggleControl(); // Toggle Player control
}

protected virtual void Update() {

if (Input.GetKeyDown(KeyCode.R))
transform.position = new Vector3(3, 0, 0); // Hit "R" to spawn in
this position

Vector3 direction = Vector3.zero;


direction += transform.forward * Input.GetAxisRaw("Vertical");
direction += transform.right * Input.GetAxisRaw("Horizontal");

direction.Normalize();

if (movementController.isGrounded) {
velocity = Vector3.zero;
} else {
velocity += -transform.up * (9.81f * 10) * Time.deltaTime; //
Gravity
}

if (Input.GetKey(KeyCode.LeftShift)) { // Player can sprint by holding


"Left Shit" keyboard button
currMoveSpeed = SprintSpeed;
} else {
currMoveSpeed = MoveSpeed;
}

direction += velocity * Time.deltaTime;


movementController.Move(direction * Time.deltaTime * currMoveSpeed);

// Camera Look
yaw += Input.GetAxisRaw("Mouse X") * LookSensitivity;
pitch -= Input.GetAxisRaw("Mouse Y") * LookSensitivity;

yaw = ClampAngle(yaw, MinYaw, MaxYaw);


pitch = ClampAngle(pitch, MinPitch, MaxPitch);

transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);


}

protected float ClampAngle(float angle) {


return ClampAngle(angle, 0, 360);
}

protected float ClampAngle(float angle, float min, float max) {


if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;

return Mathf.Clamp(angle, min, max);


}

protected void ToggleControl() {

playerCamera.gameObject.SetActive(isControlling);

#if UNITY_5
Cursor.lockState = (isControlling) ? CursorLockMode.Locked :
CursorLockMode.None;
Cursor.visible = !isControlling;
#else
Screen.lockCursor = isControlling;
#endif

You might also like