Player Controller

-Added dedicated "Mouse Look" script
-Added Ground Angle function
This commit is contained in:
juliuse98
2021-10-31 22:28:22 +01:00
parent 57ee0da640
commit b8a4d57c27
6 changed files with 159 additions and 113 deletions

View File

@@ -26,7 +26,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 681358948229890261}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0.3586286, y: -0.5799999, z: -0.03139314}
m_LocalPosition: {x: 0, y: -0.5799999, z: 0.27}
m_LocalScale: {x: 0.8, y: 0.2, z: 1}
m_Children: []
m_Father: {fileID: 981273033772084037}
@@ -266,8 +266,8 @@ GameObject:
- component: {fileID: 320375778840406984}
- component: {fileID: -4574526707603160507}
- component: {fileID: 7273209952621479910}
- component: {fileID: 5317111308216027170}
- component: {fileID: 4959463831902427506}
- component: {fileID: 5630859218361338035}
m_Layer: 0
m_Name: Player
m_TagString: Player
@@ -304,10 +304,10 @@ CharacterController:
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Height: 2
m_Radius: 0.5
m_Height: 1.8
m_Radius: 0.3
m_SlopeLimit: 45
m_StepOffset: 0.1
m_StepOffset: 0.2
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 1, z: 0}
@@ -342,21 +342,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
playerCamera: {fileID: 6272346181657429396}
playerNeck: {fileID: 981273033772084037}
mouseSensitivity: 4
maxCameraAngle: 90
neckStartAngle: 0
minCameraAngle: -90
neckLength: 0.2
mouseSmoothTime: 0.001
lockCursor: 1
walkSpeed: 6
moveSmoothTime: 0.05
gravity: -13
jumpHeight: 2
groundCheck: {fileID: 6272346182417644039}
groundDistance: 0.4
groundMask:
serializedVersion: 2
m_Bits: 64
@@ -390,24 +380,6 @@ MonoBehaviour:
showGizmos: 1
showOverlay: 1
overlayColor: {r: 0, g: 0, b: 0, a: 0.5}
--- !u!114 &5317111308216027170
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6272346181302961293}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c2e457ad00c1a044a924a5bc00776df9, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
isAlive: 0
clientId: 0
username: Janis
usernameTextObj: {fileID: 4824770690127859843}
--- !u!114 &4959463831902427506
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -424,6 +396,29 @@ MonoBehaviour:
syncInterval: 0.1
color: {r: 1, g: 0, b: 0, a: 0}
renderer: {fileID: 8824871639981994016}
--- !u!114 &5630859218361338035
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6272346181302961293}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a8493f0e2b0d4bd4f98d3cd81c2db5ad, type: 3}
m_Name:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
playerCamera: {fileID: 6272346181657429396}
playerNeck: {fileID: 981273033772084037}
mouseSensitivity: 4
maxCameraAngle: 90
neckStartAngle: 0
minCameraAngle: -90
neckLength: 0.2
mouseSmoothTime: 0.001
lockCursor: 1
--- !u!1 &6272346181657429393
GameObject:
m_ObjectHideFlags: 0
@@ -478,7 +473,7 @@ Camera:
y: 0
width: 1
height: 1
near clip plane: 0.3
near clip plane: 0.1
far clip plane: 1000
field of view: 61.4
orthographic: 0
@@ -522,7 +517,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6272346182417644038}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -0, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6272346181302961296}
@@ -571,7 +566,6 @@ GameObject:
- component: {fileID: 2458562619286483438}
- component: {fileID: 390397068222218691}
- component: {fileID: 8824871639981994016}
- component: {fileID: 5472825146870239901}
m_Layer: 0
m_Name: Capsule
m_TagString: Untagged
@@ -608,7 +602,7 @@ MeshRenderer:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8690889598169456425}
m_Enabled: 1
m_Enabled: 0
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
@@ -643,17 +637,3 @@ MeshRenderer:
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!136 &5472825146870239901
CapsuleCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8690889598169456425}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
m_Radius: 0.5
m_Height: 2
m_Direction: 1
m_Center: {x: 0, y: 0, z: 0}

View File

@@ -8,97 +8,70 @@ using Mirror;
public class PlayerController : NetworkBehaviour
{
[Header("Mouse Look")]
[SerializeField] private Transform playerCamera = null;
[SerializeField] private Transform playerNeck = null;
[SerializeField] private float mouseSensitivity = 4.0f;
[SerializeField] private float maxCameraAngle = -90f;
[SerializeField] private float neckStartAngle = 0f;
[SerializeField] private float minCameraAngle = 90f;
[SerializeField] private float neckLength = 0.2f;
[SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f;
[SerializeField] private bool lockCursor = true;
[Header("Movement")]
[SerializeField] private float walkSpeed = 6.0f;
[SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.05f;
[SerializeField] float gravity = -13.0f;
[SerializeField] private float jumpHeight;
private Vector3 inputDirection = Vector3.zero;
private Vector3 moveDirection;
[Header("Ground Check")]
[SerializeField] private Transform groundCheck;
[SerializeField] private float groundDistance = 0.4f;
[SerializeField] private LayerMask groundMask;
[Header("Ground Angle")]
// [SerializeField] private Transform groundDistance;
public bool isGrounded;
private float fullPitch = 0f;
private float cameraPitch = 0f;
private float neckPitch = 0f;
private float velocityY = 0.0f;
private CharacterController controller;
private Vector2 currentDir = Vector2.zero;
private Vector2 currentDirVelocity = Vector2.zero;
private Vector2 currentMouseDelta = Vector2.zero;
private Vector2 currentMouseDeltaVelocity = Vector2.zero;
private void Start()
{
if (isLocalPlayer)
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
else
{
playerCamera.gameObject.SetActive(false);
}
}
private void Update()
{
if (!isLocalPlayer) return;
UpdateMouseLook();
if (isLocalPlayer)
{
Grounded();
CheckGoundAngle();
UpdateMovement();
}
}
private void Grounded()
{
//Check every frame if the player stands on the ground
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
isGrounded = Physics.CheckSphere(groundCheck.position + new Vector3(0, GetComponent<CharacterController>().radius - 0.01f, 0),GetComponent<CharacterController>().radius + 0.0f, groundMask);
}
private void UpdateMouseLook()
private void CheckGoundAngle()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //Get the axis of the mouse
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
fullPitch -= currentMouseDelta.y * mouseSensitivity;
fullPitch = Mathf.Clamp(fullPitch,-maxCameraAngle,-minCameraAngle);
if (fullPitch >= neckStartAngle) {
playerNeck.localEulerAngles = Vector3.right * (fullPitch - neckStartAngle);
//Check every frame if the player stands on the ground
RaycastHit hit;
if (Physics.Raycast(groundCheck.position + new Vector3(0,0.4f,0),Vector3.down,out hit))
{
Debug.Log(transform.eulerAngles.y);
moveDirection = Quaternion.Euler(0, transform.eulerAngles.y + 90, 0) * inputDirection;
moveDirection = Vector3.Cross(moveDirection, hit.normal);
}
else {
playerCamera.localEulerAngles = Vector3.right * fullPitch;
}
playerCamera.position = playerNeck.position;
playerCamera.position += playerNeck.up * neckLength;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); //Rotate the hole player if looked sideways (Rotates the player left and right)
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawRay(new Ray(transform.position, moveDirection * 50));
}
private void UpdateMovement()
{
//Grounded
@@ -113,10 +86,10 @@ public class PlayerController : NetworkBehaviour
velocityY += Mathf.Sqrt(jumpHeight * -2f * gravity);
}
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); //Get Inputs
targetDir.Normalize(); //Damit schr<68>g laufen nicht schneller ist
inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical")); //Get Inputs
inputDirection.Normalize(); //Damit schr<68>g laufen nicht schneller ist
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime); //Smooth movement change
currentDir = Vector2.SmoothDamp(currentDir, new Vector2(inputDirection.x,inputDirection.z), ref currentDirVelocity, moveSmoothTime); //Smooth movement change
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + (Vector3.up * velocityY);

View File

@@ -0,0 +1,82 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class PlayerMouseLook : NetworkBehaviour
{
[Header("Mouse Look")]
[SerializeField] private Transform playerCamera = null;
[SerializeField] private Transform playerNeck = null;
[SerializeField] private float mouseSensitivity = 4.0f;
[SerializeField] private float maxCameraAngle = 90f;
[SerializeField] private float neckStartAngle = 0f;
[SerializeField] private float minCameraAngle = -90f;
[SerializeField] private float neckLength = 0.2f;
[SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f;
[SerializeField] private bool lockCursor = true;
private float fullPitch = 0f;
private float cameraPitch = 0f;
private float neckPitch = 0f;
private float velocityY = 0.0f;
private CharacterController controller;
private Vector2 currentMouseDelta = Vector2.zero;
private Vector2 currentMouseDeltaVelocity = Vector2.zero;
private void Start()
{
if (isLocalPlayer)
{
controller = GetComponent<CharacterController>();
playerCamera.gameObject.SetActive(true);
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
}
private void Update()
{
if (isLocalPlayer)
{
UpdateMouseLook();
}
}
private void UpdateMouseLook()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); //Get the axis of the mouse
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
fullPitch -= currentMouseDelta.y * mouseSensitivity;
fullPitch = Mathf.Clamp(fullPitch, -maxCameraAngle, -minCameraAngle);
if (fullPitch >= neckStartAngle)
{
playerNeck.localEulerAngles = Vector3.right * (fullPitch - neckStartAngle);
}
else
{
playerCamera.localEulerAngles = Vector3.right * fullPitch;
}
playerCamera.position = playerNeck.position;
playerCamera.position += playerNeck.up * neckLength;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity); //Rotate the hole player if looked sideways (Rotates the player left and right)
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a8493f0e2b0d4bd4f98d3cd81c2db5ad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,2 +1,2 @@
m_EditorVersion: 2021.1.26f1
m_EditorVersionWithRevision: 2021.1.26f1 (6f076387c01d)
m_EditorVersion: 2021.1.24f1
m_EditorVersionWithRevision: 2021.1.24f1 (6667702a1e7c)

View File

@@ -9,10 +9,10 @@ EditorUserSettings:
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d
flags: 0
RecentlyUsedScenePath-1:
value: 22424703114646680e0b0227036c6b1505032b292926237e38271427fb
value: 22424703114646680e0b0227036c6b150503372c2a242f3e281a1e36ece57a2decee22f0
flags: 0
RecentlyUsedScenePath-2:
value: 22424703114646680e0b0227036c6b150503372c2a242f3e281a1e36ece57a2decee22f0
value: 22424703114646680e0b0227036c6b1505032b292926237e38271427fb
flags: 0
vcSharedLogLevel:
value: 0d5e400f0650