Added Animations. Changed inspector view for navpoints. A lot of fixes. Changed project structure.
This commit is contained in:
@ -0,0 +1,253 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class scr_CharacterController : MonoBehaviour
|
||||
{
|
||||
private CharacterController characterController;
|
||||
private DefaultInput defaultInput;
|
||||
private Vector2 input_Movement;
|
||||
[HideInInspector]
|
||||
public Vector2 input_View;
|
||||
|
||||
private Vector3 newCameraRotation;
|
||||
private Vector3 newCharacterRotation;
|
||||
|
||||
[Header("References")]
|
||||
public Transform cameraHolder;
|
||||
public Transform feetTransform;
|
||||
|
||||
[Header("Settings")]
|
||||
public PlayerSettingsModel playerSettings;
|
||||
|
||||
public float ViewClampYMin = -70;
|
||||
public float ViewClampYMax = 80;
|
||||
public LayerMask playerMask;
|
||||
|
||||
[Header("Gravity")]
|
||||
public float gravityAmount;
|
||||
public float gravityMin;
|
||||
private float playerGravity;
|
||||
|
||||
public Vector3 jumpingForce;
|
||||
private Vector3 jumpingForceVelocity;
|
||||
|
||||
[Header("Stance")]
|
||||
public PlayerStance playerStance;
|
||||
public float playerStanceSmoothing;
|
||||
public CharacterStance playerStandStance;
|
||||
public CharacterStance playerCrouchStance;
|
||||
public CharacterStance playerProneStance;
|
||||
private float stanceCheckErrorMargin = 0.05f;
|
||||
|
||||
private float cameraHeight;
|
||||
private float cameraHeightVelocity;
|
||||
|
||||
private bool isSprinting;
|
||||
|
||||
private Vector3 newMovementSpeed;
|
||||
private Vector3 newMovementSpeedVelocity;
|
||||
|
||||
[Header("Weapon")]
|
||||
public scr_WeaponController currentWeapon;
|
||||
private void Awake()
|
||||
{
|
||||
defaultInput = new DefaultInput();
|
||||
|
||||
defaultInput.Character.Movement.performed += e => input_Movement = e.ReadValue<Vector2>();
|
||||
defaultInput.Character.View.performed += e => input_View = e.ReadValue<Vector2>();
|
||||
defaultInput.Character.Jump.performed += e => Jump();
|
||||
|
||||
defaultInput.Character.Crouch.performed += e => Crouch();
|
||||
defaultInput.Character.Prone.performed += e => Prone();
|
||||
|
||||
defaultInput.Character.Sprint.performed += e => ToggleSprint();
|
||||
defaultInput.Character.SprintReleased.performed += e => StopSprint();
|
||||
|
||||
defaultInput.Enable();
|
||||
|
||||
newCameraRotation = cameraHolder.localRotation.eulerAngles;
|
||||
newCharacterRotation = transform.localRotation.eulerAngles;
|
||||
characterController = GetComponent<CharacterController>();
|
||||
|
||||
cameraHeight = cameraHolder.localPosition.y;
|
||||
|
||||
if (currentWeapon)
|
||||
{
|
||||
currentWeapon.Initialise(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
CalculateView();
|
||||
CalculateMovement();
|
||||
CalculateJump();
|
||||
CalculateCameraHeight();
|
||||
}
|
||||
|
||||
private void CalculateView()
|
||||
{
|
||||
newCharacterRotation.y += playerSettings.ViewXSensetivity * (playerSettings.ViewXInverted ? -input_View.x : input_View.x) * Time.deltaTime;
|
||||
transform.localRotation = Quaternion.Euler(newCharacterRotation);
|
||||
|
||||
newCameraRotation.x += playerSettings.ViewYSensetivity * (playerSettings.ViewYInverted ? input_View.y : -input_View.y) * Time.deltaTime;
|
||||
newCameraRotation.x = Mathf.Clamp(newCameraRotation.x, ViewClampYMin, ViewClampYMax);
|
||||
|
||||
cameraHolder.localRotation = Quaternion.Euler(newCameraRotation);
|
||||
}
|
||||
|
||||
private void CalculateMovement()
|
||||
{
|
||||
if (input_Movement.y <= 0.2f)
|
||||
{
|
||||
isSprinting = false;
|
||||
}
|
||||
|
||||
var verticalSpeed = playerSettings.WalkingForwardSpeed;
|
||||
var horizontalSpeed = playerSettings.WalkingStrafeSpeed;
|
||||
|
||||
if (isSprinting)
|
||||
{
|
||||
verticalSpeed = playerSettings.RunningForwardSpeed;
|
||||
horizontalSpeed = playerSettings.RunningStrafeSpeed;
|
||||
}
|
||||
|
||||
// Effectors
|
||||
if (!characterController.isGrounded)
|
||||
{
|
||||
playerSettings.SpeedEffector = playerSettings.FallingSpeedEffector;
|
||||
}
|
||||
else if (playerStance == PlayerStance.Crouch)
|
||||
{
|
||||
playerSettings.SpeedEffector = playerSettings.CrouchSpeedEffector;
|
||||
}
|
||||
else if (playerStance == PlayerStance.Prone)
|
||||
{
|
||||
playerSettings.SpeedEffector = playerSettings.ProneSpeedEffector;
|
||||
}
|
||||
else
|
||||
{
|
||||
playerSettings.SpeedEffector = 1;
|
||||
}
|
||||
|
||||
verticalSpeed *= playerSettings.SpeedEffector;
|
||||
horizontalSpeed *= playerSettings.SpeedEffector;
|
||||
|
||||
newMovementSpeed = Vector3.SmoothDamp(newMovementSpeed,
|
||||
new Vector3(horizontalSpeed * input_Movement.x * Time.deltaTime,
|
||||
0, verticalSpeed * input_Movement.y * Time.deltaTime),
|
||||
ref newMovementSpeedVelocity, characterController.isGrounded ? playerSettings.MovementSmoothing : playerSettings.FallingSmoothing);
|
||||
|
||||
var MovementSpeed = transform.TransformDirection(newMovementSpeed);
|
||||
|
||||
if (playerGravity > gravityMin)
|
||||
{
|
||||
playerGravity -= gravityAmount * Time.deltaTime;
|
||||
}
|
||||
|
||||
if (playerGravity < -0.1f && characterController.isGrounded)
|
||||
{
|
||||
playerGravity = -0.1f;
|
||||
}
|
||||
|
||||
MovementSpeed.y += playerGravity;
|
||||
MovementSpeed += jumpingForce * Time.deltaTime;
|
||||
|
||||
characterController.Move(MovementSpeed);
|
||||
}
|
||||
|
||||
private void CalculateJump()
|
||||
{
|
||||
jumpingForce = Vector3.SmoothDamp(jumpingForce, Vector3.zero, ref jumpingForceVelocity, playerSettings.JumpingFalloff);
|
||||
}
|
||||
|
||||
private void CalculateCameraHeight()
|
||||
{
|
||||
var stanceHeight = playerStandStance.CameraHeight;
|
||||
|
||||
if (playerStance == PlayerStance.Crouch)
|
||||
{
|
||||
stanceHeight = playerCrouchStance.CameraHeight;
|
||||
}
|
||||
else if (playerStance == PlayerStance.Prone)
|
||||
{
|
||||
stanceHeight = playerProneStance.CameraHeight;
|
||||
}
|
||||
|
||||
cameraHeight = Mathf.SmoothDamp(cameraHolder.localPosition.y, stanceHeight, ref cameraHeightVelocity, playerStanceSmoothing);
|
||||
|
||||
cameraHolder.localPosition = new Vector3(cameraHolder.localPosition.x, cameraHeight, cameraHolder.localPosition.z);
|
||||
}
|
||||
private void Jump()
|
||||
{
|
||||
if (!characterController.isGrounded || playerStance == PlayerStance.Prone)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (playerStance == PlayerStance.Crouch)
|
||||
{
|
||||
if (StanceCheck(playerStandStance.StanceCollider.height))
|
||||
{
|
||||
return;
|
||||
}
|
||||
playerStance = PlayerStance.Stand;
|
||||
return;
|
||||
}
|
||||
|
||||
// Jump
|
||||
jumpingForce = Vector3.up * playerSettings.JumpingHeight;
|
||||
playerGravity = 0;
|
||||
}
|
||||
|
||||
private void Crouch()
|
||||
{
|
||||
if (playerStance == PlayerStance.Crouch)
|
||||
{
|
||||
if (StanceCheck(playerStandStance.StanceCollider.height))
|
||||
{
|
||||
return;
|
||||
}
|
||||
playerStance = PlayerStance.Stand;
|
||||
return;
|
||||
}
|
||||
if (StanceCheck(playerCrouchStance.StanceCollider.height))
|
||||
{
|
||||
return;
|
||||
}
|
||||
playerStance = PlayerStance.Crouch;
|
||||
}
|
||||
|
||||
private void Prone()
|
||||
{
|
||||
playerStance = PlayerStance.Prone;
|
||||
}
|
||||
|
||||
private bool StanceCheck(float stanceCheckheight)
|
||||
{
|
||||
var start = new Vector3(feetTransform.position.x, feetTransform.position.y + characterController.radius + stanceCheckErrorMargin, feetTransform.position.z);
|
||||
var end = new Vector3(feetTransform.position.x, feetTransform.position.y - characterController.radius - stanceCheckErrorMargin + stanceCheckheight, feetTransform.position.z);
|
||||
|
||||
|
||||
return Physics.CheckCapsule(start, end, characterController.radius, playerMask);
|
||||
}
|
||||
|
||||
private void ToggleSprint()
|
||||
{
|
||||
if (input_Movement.y <= 0.2f)
|
||||
{
|
||||
isSprinting = false;
|
||||
return;
|
||||
}
|
||||
isSprinting = !isSprinting;
|
||||
}
|
||||
|
||||
private void StopSprint()
|
||||
{
|
||||
if (playerSettings.SprintingHold)
|
||||
{
|
||||
isSprinting = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/Animators/Kirill Animator/CustomCharacterController.cs.meta
generated
Normal file
11
Assets/Scripts/Animators/Kirill Animator/CustomCharacterController.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9826297ef4d853741b2af768441ec7f7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
67
Assets/Scripts/Animators/Kirill Animator/Models.cs
Normal file
67
Assets/Scripts/Animators/Kirill Animator/Models.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public enum PlayerStance
|
||||
{
|
||||
Stand,
|
||||
Crouch,
|
||||
Prone
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class PlayerSettingsModel
|
||||
{
|
||||
[Header("View Settings")]
|
||||
public float ViewXSensetivity;
|
||||
public float ViewYSensetivity;
|
||||
|
||||
public bool ViewXInverted;
|
||||
public bool ViewYInverted;
|
||||
|
||||
[Header("Movement Settings")]
|
||||
public bool SprintingHold;
|
||||
public float MovementSmoothing;
|
||||
|
||||
[Header("Movement - Running")]
|
||||
public float RunningForwardSpeed;
|
||||
public float RunningStrafeSpeed;
|
||||
|
||||
[Header("Movement - Walking")]
|
||||
public float WalkingForwardSpeed;
|
||||
public float WalkingBackwardSpeed;
|
||||
public float WalkingStrafeSpeed;
|
||||
|
||||
[Header("Jumping")]
|
||||
public float JumpingHeight;
|
||||
public float JumpingFalloff;
|
||||
public float FallingSmoothing;
|
||||
|
||||
[Header("Speed Effectors")]
|
||||
public float SpeedEffector = 1;
|
||||
public float CrouchSpeedEffector;
|
||||
public float ProneSpeedEffector;
|
||||
public float FallingSpeedEffector;
|
||||
}
|
||||
|
||||
|
||||
[Serializable]
|
||||
public class CharacterStance
|
||||
{
|
||||
public float CameraHeight;
|
||||
public CapsuleCollider StanceCollider;
|
||||
}
|
||||
|
||||
|
||||
[Serializable]
|
||||
public class WeaponSettingsModel
|
||||
{
|
||||
[Header("Sway")]
|
||||
public float SwayAmount;
|
||||
public bool SwayYInverted;
|
||||
public bool SwayXInverted;
|
||||
public float SwaySmoothing;
|
||||
public float SwayResetSmoothing;
|
||||
public float SwayClampX;
|
||||
public float SwayClampY;
|
||||
}
|
11
Assets/Scripts/Animators/Kirill Animator/Models.cs.meta
generated
Normal file
11
Assets/Scripts/Animators/Kirill Animator/Models.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 907ff02de47a55a4e971d73d25e7d006
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user