mirror of
https://github.com/PashaBibko/The-Mobius-Line.git
synced 2026-04-04 01:49:07 +00:00
Added basic movement
This commit is contained in:
69
Assets/Scripts/Player/CameraController.cs
Normal file
69
Assets/Scripts/Player/CameraController.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using UnityEngine;
|
||||
|
||||
// Stops errors from happening by making sure there is a Camera in the same group of components
|
||||
[RequireComponent(typeof(Camera))]
|
||||
|
||||
// Controller from the Player Camera, NOT FOR PORTAL CAMERAS
|
||||
public class CameraController : MonoBehaviour
|
||||
{
|
||||
[Header("Settings")]
|
||||
[SerializeField] Vector2 m_Sensitivity;
|
||||
[SerializeField] float m_MaxAngle;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] Transform m_Orientation;
|
||||
|
||||
// Private variables //
|
||||
|
||||
// Current angle the player is looking at
|
||||
Vector2 m_Rotation;
|
||||
|
||||
// The instance of the camera
|
||||
static CameraController s_Instance = null;
|
||||
|
||||
// Provides a way for external objects to interact with the camera
|
||||
public CameraController Instance() => s_Instance;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
// Checks it is the only instance
|
||||
if (s_Instance == null)
|
||||
{
|
||||
// Sets itself as the instance
|
||||
s_Instance = this;
|
||||
}
|
||||
|
||||
// Else throws an error in the console
|
||||
else
|
||||
{
|
||||
Debug.LogError("Multiple instances of CameraController within Scene");
|
||||
}
|
||||
|
||||
// Locks the cursor and makes it invisible
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
// Gets the mouse input from the user
|
||||
Vector2 mouse = new Vector2
|
||||
(
|
||||
Input.GetAxisRaw("Mouse X") * Time.deltaTime * m_Sensitivity.x,
|
||||
Input.GetAxisRaw("Mouse Y") * Time.deltaTime * m_Sensitivity.y
|
||||
);
|
||||
|
||||
// Applies the mouse movement to the camera angle
|
||||
m_Rotation.x -= mouse.y; // Yes these lines look cursed but blame Unity not me
|
||||
m_Rotation.y += mouse.x;
|
||||
|
||||
// Clamps the angle to stop the camera from looping around
|
||||
m_Rotation.x = Mathf.Clamp(m_Rotation.x, -m_MaxAngle, m_MaxAngle);
|
||||
|
||||
// Applies the rotation to the objects within Unity
|
||||
m_Orientation.rotation = Quaternion.Euler(0, m_Rotation.y, 0);
|
||||
transform.rotation = Quaternion.Euler(m_Rotation.x, m_Rotation.y, 0);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/CameraController.cs.meta
Normal file
11
Assets/Scripts/Player/CameraController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5cfd42fc2ed2f504dae2b9e7095401cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
117
Assets/Scripts/Player/PlayerMovement.cs
Normal file
117
Assets/Scripts/Player/PlayerMovement.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[Header("General Settings")]
|
||||
[SerializeField] float m_MoveSpeed;
|
||||
[SerializeField] float m_GroundDrag;
|
||||
|
||||
[Header("Ground Check")]
|
||||
[SerializeField] float m_PlayerHeight;
|
||||
[SerializeField] LayerMask m_GroundMask;
|
||||
|
||||
[Header("Jump Settings")]
|
||||
[SerializeField] float m_JumpForce;
|
||||
|
||||
[Header("KeyBinds")]
|
||||
[SerializeField] KeyCode m_JumpKey;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] Rigidbody m_Body;
|
||||
[SerializeField] Transform m_Orientation;
|
||||
|
||||
//
|
||||
Vector2 m_Input;
|
||||
|
||||
//
|
||||
Vector3 m_MoveDir;
|
||||
|
||||
// Is the player on the ground
|
||||
bool m_Grounded;
|
||||
|
||||
bool m_JumpKeyPressed;
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
// Stops the rigidbody from rotatating when we don't want it to
|
||||
m_Body.freezeRotation = true;
|
||||
}
|
||||
|
||||
// Updates the state of the user input
|
||||
private void UpdateInput()
|
||||
{
|
||||
// Calls get axis raw to ignore any uneeded scaling
|
||||
m_Input.x = Input.GetAxisRaw("Horizontal");
|
||||
m_Input.y = Input.GetAxisRaw("Vertical");
|
||||
|
||||
// Checks wether the jump button has been pressed
|
||||
m_JumpKeyPressed = Input.GetKey(m_JumpKey);
|
||||
}
|
||||
|
||||
// Applies drag to the player
|
||||
private void ApplyDrag()
|
||||
{
|
||||
// Only applies ground drag if the player is on the floor
|
||||
if (m_Grounded)
|
||||
{
|
||||
m_Body.drag = m_GroundDrag;
|
||||
}
|
||||
|
||||
// Else there is no drag on the player
|
||||
else
|
||||
{
|
||||
m_Body.drag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
// Updates wether the player is touching the ground or not
|
||||
m_Grounded = Physics.Raycast(transform.position, Vector3.down, m_PlayerHeight * 0.5f + 0.2f, m_GroundMask);
|
||||
|
||||
// Updates the state of the user input
|
||||
UpdateInput();
|
||||
|
||||
// Applies drag to the player
|
||||
ApplyDrag();
|
||||
}
|
||||
|
||||
// Updates basic movement and player jumping
|
||||
private void UpdatePlayerPosition()
|
||||
{
|
||||
// Calculates the movement direction
|
||||
m_MoveDir = (m_Orientation.forward * m_Input.y) + (m_Orientation.right * m_Input.x);
|
||||
|
||||
// Adds the force to the rigid body
|
||||
m_Body.AddForce(m_MoveDir.normalized * m_MoveSpeed * 10.0f, ForceMode.Force);
|
||||
|
||||
// Jumps if the jump key has been pressed
|
||||
if (m_JumpKeyPressed)
|
||||
{
|
||||
// The jump function stops jumping if not grounded so no check is needed here
|
||||
Jump();
|
||||
}
|
||||
}
|
||||
|
||||
// Function to make the player jump
|
||||
// The function checks wether the player is grounded so external checks are not needed
|
||||
private void Jump(bool force = false)
|
||||
{
|
||||
// Checks wether the player is grounded
|
||||
// Can be overriden by passing true to force a jump
|
||||
if (m_Grounded || force)
|
||||
{
|
||||
// Applies an upwards force simulating a jump
|
||||
m_Body.AddForce(transform.up * m_JumpForce, ForceMode.Impulse);
|
||||
}
|
||||
}
|
||||
|
||||
// Fixed Update is called once per physics update
|
||||
private void FixedUpdate()
|
||||
{
|
||||
// Updates the position of the player
|
||||
UpdatePlayerPosition();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/PlayerMovement.cs.meta
Normal file
11
Assets/Scripts/Player/PlayerMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae4c295274f6a0645b04723c79481e73
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user