mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
I Think i Fixed Everyting
This commit is contained in:
@@ -8,7 +8,7 @@ using Mirror;
|
||||
|
||||
public class PlayerController : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private AimDownSights ADSContoller;
|
||||
[SerializeField] private ProcedualAnimationController procedualAnimationController;
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float walkSpeed = 6.0f;
|
||||
[SerializeField] private float sprintSpeed = 10.0f;
|
||||
@@ -36,7 +36,8 @@ public class PlayerController : NetworkBehaviour
|
||||
|
||||
private Vector3 currentDir = Vector3.zero;
|
||||
private Vector3 currentDirVelocity = Vector3.zero;
|
||||
private Vector3 velocity = Vector3.zero;
|
||||
public Vector3 velocity = Vector3.zero;
|
||||
private Vector3 refVelocity = Vector3.zero;
|
||||
|
||||
|
||||
private void Start()
|
||||
@@ -65,8 +66,8 @@ public class PlayerController : NetworkBehaviour
|
||||
|
||||
public bool isMoving()
|
||||
{
|
||||
if (velocity.x == 0 && velocity.y == 0 && velocity.z == 0) return true;
|
||||
else return false;
|
||||
if (velocity.x == 0 && velocity.y == 0 && velocity.z == 0) return false;
|
||||
else return true;
|
||||
}
|
||||
|
||||
private void CheckGoundAngle()
|
||||
@@ -101,16 +102,18 @@ public class PlayerController : NetworkBehaviour
|
||||
{
|
||||
|
||||
|
||||
if (Input.GetAxisRaw("Sprint") > 0 && isGrounded && !ADSContoller.isAiming)
|
||||
{
|
||||
if (Input.GetAxisRaw("Sprint") > 0 && isGrounded && !procedualAnimationController.isAiming)
|
||||
{
|
||||
movementSpeed = sprintSpeed;
|
||||
isSprinting = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(ADSContoller.isAiming) movementSpeed = aimWalkSpeed;
|
||||
|
||||
if(procedualAnimationController.isAiming) movementSpeed = aimWalkSpeed;
|
||||
else movementSpeed = walkSpeed;
|
||||
isSprinting = false;
|
||||
|
||||
}
|
||||
|
||||
//Grounded
|
||||
@@ -133,6 +136,7 @@ public class PlayerController : NetworkBehaviour
|
||||
}
|
||||
|
||||
inputDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical")); //Get Inputs
|
||||
|
||||
inputDirection.Normalize(); //Damit schr<68>g laufen nicht schneller ist
|
||||
|
||||
if (isGrounded)
|
||||
@@ -145,11 +149,8 @@ public class PlayerController : NetworkBehaviour
|
||||
currentDir = moveDirection;
|
||||
}
|
||||
|
||||
velocity = currentDir * movementSpeed + new Vector3(0, velocityY, 0);
|
||||
|
||||
|
||||
velocity = Vector3.SmoothDamp(velocity, currentDir * movementSpeed + new Vector3(0, velocityY, 0),ref refVelocity,0.01f);
|
||||
controller.Move(velocity * Time.deltaTime);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class PlayerMouseLook : NetworkBehaviour
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
|
||||
playerCamera.gameObject.SetActive(true);
|
||||
playerCamera.GetComponent<Camera>().enabled = true;
|
||||
neckLength = Vector3.Distance(playerNeck.position,playerCamera.position);
|
||||
|
||||
if (lockCursor)
|
||||
@@ -52,7 +52,7 @@ public class PlayerMouseLook : NetworkBehaviour
|
||||
{
|
||||
UpdateMouseLook();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void UpdateMouseLook()
|
||||
|
||||
306
Assets/Scripts/Player/ProcedualAnimationController.cs
Normal file
306
Assets/Scripts/Player/ProcedualAnimationController.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
|
||||
public class ProcedualAnimationController : NetworkBehaviour
|
||||
{
|
||||
|
||||
[Header("Step Settings")]
|
||||
[SerializeField] private float stepAmplitudeWalking;
|
||||
[SerializeField] private float stepAmplitudeSprinting;
|
||||
[SerializeField] private float stepFrequency;
|
||||
private float checkDist = 0f;
|
||||
private float stepDistance = 1;
|
||||
private float stepSin;
|
||||
float moveSoftStart = 0;
|
||||
Vector3 lastPos = Vector3.zero;
|
||||
float posCheckDistance = 0.01f;
|
||||
float currentDist = 0;
|
||||
|
||||
|
||||
float f = 0f;
|
||||
|
||||
[SerializeField] private PlayerController playerController;
|
||||
|
||||
|
||||
|
||||
|
||||
[Header("GameObjects")]
|
||||
[SerializeField] private float currentCameraRecoilX = 0f;
|
||||
[SerializeField] private float currentCameraRecoilY = 0f;
|
||||
[Header("GameObjects")]
|
||||
[SerializeField] private GameObject gun; // The gun object with the animator on it.
|
||||
[SerializeField] private GameObject gunHolder;
|
||||
|
||||
|
||||
[Header("General Settings")]
|
||||
[SerializeField] bool positionRecoil = true;
|
||||
[SerializeField] bool rotationRecoil = true;
|
||||
|
||||
[Header("Position Settings")]
|
||||
[SerializeField] float positionMultX = 25f;
|
||||
[SerializeField] float positionMultY = 25f;
|
||||
[SerializeField] float positionMultZ = 25f;
|
||||
|
||||
[Header("Rotation Settings")]
|
||||
[SerializeField] PlayerMouseLook playerMouseLook;
|
||||
[SerializeField] float cameraRecoilX = 0.1f;
|
||||
[SerializeField] float cameraRecoilY = 0.1f;
|
||||
|
||||
|
||||
[SerializeField] bool rotX = true;
|
||||
[SerializeField] float rotationMultX = 25f;
|
||||
[SerializeField] float rotationOffsetX = 0.1f;
|
||||
[SerializeField] bool rotY = true;
|
||||
[SerializeField] float rotationMultY = 25f;
|
||||
[SerializeField] bool rotZ = true;
|
||||
[SerializeField] float rotationMultZ = 15f;
|
||||
|
||||
[Header("Swey Settings")]
|
||||
[SerializeField] bool sideSwey = true;
|
||||
[SerializeField] float sweyMultX = 15f;
|
||||
[SerializeField] float sweyMultY = 15f;
|
||||
[SerializeField] float sweyMultZ = 15f;
|
||||
[SerializeField] float sweyWhileAim = 0.1f;
|
||||
float swey = 0f;
|
||||
|
||||
[Header("External Settings")]
|
||||
[SerializeField] const int externalPositionVectorsNum = 1;
|
||||
private Vector3[] externalPositionVectors = new Vector3[externalPositionVectorsNum];
|
||||
|
||||
|
||||
[SerializeField] float returnForce = 0.006f;
|
||||
[SerializeField] float impulsForce = 0.025f;
|
||||
[SerializeField] float maxRecoil = 0.1f;
|
||||
|
||||
private Animator gunAnimator;
|
||||
|
||||
Vector3 startPos, startRot;
|
||||
float recoilOffset = 0f;
|
||||
float zOffset = 0f;
|
||||
float zVelocity = 0f;
|
||||
|
||||
int recoilCounter = 0;
|
||||
|
||||
[Header("Aiming Settings")]
|
||||
[SerializeField] float aimSpeed = 0.01f;
|
||||
[Range(0, 1)] public float aimVal = 0;
|
||||
[SerializeField] GameObject AimPoint;
|
||||
[SerializeField] GameObject HoldPoint;
|
||||
public bool isAiming = false;
|
||||
|
||||
Vector3[] positionMod = new Vector3[3];
|
||||
Quaternion[] rotationMod = new Quaternion[3];
|
||||
|
||||
|
||||
public void OnSwitchWeapon(float fireRate)
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
//gun = newGun;
|
||||
gunAnimator = gun.GetComponent<Animator>();
|
||||
gunAnimator.SetFloat("ShootSpeed", 1f / (60f / fireRate));
|
||||
//startPos = gunPositionObj.transform.localPosition;
|
||||
//startRot = gunRotationObj.transform.localRotation.eulerAngles;
|
||||
}
|
||||
}
|
||||
|
||||
public void Recoil(float force)
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
//Play the animation
|
||||
gunAnimator.Play("Shoot");
|
||||
//Add force for the recoil
|
||||
//currentCameraRecoilX -= weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1);
|
||||
//playerMouseLook = Mathf.SmoothDamp(playerMouseLook.fullPitch, playerMouseLook.fullPitch - weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1),ref f,0.01f);
|
||||
CmdRecoil();
|
||||
}
|
||||
}
|
||||
[Command]
|
||||
private void CmdRecoil()
|
||||
{
|
||||
recoilCounter++;
|
||||
playerMouseLook.fullPitch -= weightedPerlinNoise(cameraRecoilX, 1f, Time.time, 1);
|
||||
transform.Rotate(Vector3.up * weightedPerlinNoise(cameraRecoilY, 1f, Time.time, 1f, -0.5f));
|
||||
}
|
||||
[Command]
|
||||
private void CmdAim(bool input)
|
||||
{
|
||||
if (input) isAiming = true;
|
||||
else isAiming = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
CmdAim(Input.GetButton("Aim"));
|
||||
}
|
||||
/*-----Aiming-----*/
|
||||
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (isServer)
|
||||
{
|
||||
positionMod = new Vector3[3];
|
||||
rotationMod = new Quaternion[3];
|
||||
/*-----Recoil-----*/
|
||||
calcRecoilOffset();
|
||||
/*-----Position Recoil-----*/
|
||||
PositionRecoil();
|
||||
/*-----Rotation Recoil-----*/
|
||||
RotationRecoil();
|
||||
/*-----Step Swey-----*/
|
||||
calcStepSin();
|
||||
SideStep();
|
||||
/*-----Aiming-----*/
|
||||
AimDownSights();
|
||||
/*-----Apply Gun Position-----*/
|
||||
Vector3 totalPosition = Vector3.zero;
|
||||
for (int i = 0; i < positionMod.Length; i++)
|
||||
{
|
||||
if (positionMod[i] != null)
|
||||
totalPosition += positionMod[i];
|
||||
}
|
||||
|
||||
/*-----Apply Gun Rotation-----*/
|
||||
Quaternion totalRotation = Quaternion.identity;
|
||||
for (int i = 0; i < rotationMod.Length; i++)
|
||||
{
|
||||
totalRotation *= rotationMod[i];
|
||||
}
|
||||
//gunHolder.transform.localPosition = positionMod[2];
|
||||
gunHolder.transform.localPosition = totalPosition;
|
||||
//gunHolder.transform.localRotation = totalRotation;
|
||||
gunHolder.transform.localRotation = rotationMod[0];
|
||||
}
|
||||
}
|
||||
|
||||
void RotationRecoil()
|
||||
{
|
||||
/*-----Rotation Recoil-----*/
|
||||
if (rotationRecoil)
|
||||
{
|
||||
float deltaX = 100f * rotationMultX * weightedPerlinNoise(recoilOffset, 0.5f, Time.time, 1f);
|
||||
float deltaY = 100f * rotationMultY * weightedPerlinNoise(recoilOffset, 0.5f, Time.time, 1f, -0.5f);
|
||||
float deltaZ = 100f * rotationMultZ * weightedPerlinNoise(recoilOffset, 0.5f, Time.time, 1f, -0.5f);
|
||||
|
||||
rotationMod[0] = Quaternion.Euler(deltaX, deltaY, deltaZ);
|
||||
}
|
||||
}
|
||||
void PositionRecoil()
|
||||
{
|
||||
if (positionRecoil)
|
||||
{
|
||||
float deltaX = -positionMultX * weightedPerlinNoise(recoilOffset, 0.7f, Time.time, 1f, -0.5f);
|
||||
float deltaY = -positionMultY * weightedPerlinNoise(recoilOffset, 0.5f, Time.time, 2f);
|
||||
float deltaZ = positionMultZ * weightedPerlinNoise(recoilOffset, 0.5f, Time.time, 3f);
|
||||
|
||||
positionMod[0] = new Vector3(deltaX, deltaY, deltaZ);
|
||||
}
|
||||
}
|
||||
void AimDownSights()
|
||||
{
|
||||
aimVal = gravityValue(aimVal, aimSpeed, 1, 0, isAiming);
|
||||
positionMod[2] = Vector3.Lerp(HoldPoint.transform.localPosition, AimPoint.transform.localPosition, Mathf.Pow(aimVal, 1.3f));
|
||||
}
|
||||
void calcRecoilOffset()
|
||||
{
|
||||
for (int i = 0; i < recoilCounter; i++)
|
||||
{
|
||||
zVelocity -= weightedPerlinNoise(impulsForce, 0.1f, i, 1f);
|
||||
}
|
||||
recoilCounter = 0;
|
||||
|
||||
recoilOffset += zVelocity;
|
||||
|
||||
zVelocity = 0;
|
||||
|
||||
if (recoilOffset > 0)
|
||||
recoilOffset = 0f;
|
||||
else if (recoilOffset < 0)
|
||||
zVelocity += weightedPerlinNoise(returnForce, 0.1f, Time.time, 1f);
|
||||
|
||||
recoilOffset = Mathf.Clamp(recoilOffset, -weightedPerlinNoise(maxRecoil, 0.5f, Time.time * 1000, 1f), 0);
|
||||
|
||||
}
|
||||
void calcStepSin()
|
||||
{
|
||||
float amplitude;
|
||||
|
||||
float dist = Vector3.Distance(lastPos, this.transform.position);
|
||||
if (playerController.isSprinting)
|
||||
amplitude = stepAmplitudeSprinting;
|
||||
else
|
||||
amplitude = stepAmplitudeWalking;
|
||||
|
||||
if (dist > posCheckDistance)
|
||||
{
|
||||
currentDist += dist;
|
||||
lastPos = this.transform.position;
|
||||
}
|
||||
else
|
||||
{
|
||||
checkDist = currentDist + dist;
|
||||
}
|
||||
stepSin = ezSin(stepAmplitudeWalking, stepFrequency, currentDist);
|
||||
}
|
||||
void SideStep()
|
||||
{
|
||||
if (sideSwey)
|
||||
{
|
||||
//To start and end the sweying motion softly
|
||||
|
||||
moveSoftStart = gravityValue(moveSoftStart, 0.1f, 0.03f, 1f, 0f, playerController.isMoving() && playerController.isGrounded);
|
||||
|
||||
if (playerController.isMoving())
|
||||
{
|
||||
float deltaX = sweyMultX * moveSoftStart * Mathf.Clamp(Vector3.Magnitude(playerController.velocity), 0, 1) * weightedPerlinNoise(stepSin, 0.3f, Time.time, 10f, 0.5f) * Mathf.Clamp((1 - aimVal) * (1 - aimVal), sweyWhileAim, 1f);
|
||||
float deltaY = sweyMultY * moveSoftStart * Mathf.Clamp(Vector3.Magnitude(playerController.velocity), 0, 1) * weightedPerlinNoise(stepSin, 0.7f, Time.time, 20f, -0.5f) * Mathf.Clamp((1 - aimVal) * (1 - aimVal), sweyWhileAim, 1f);
|
||||
float deltaZ = sweyMultZ * moveSoftStart * Mathf.Clamp(Vector3.Magnitude(playerController.velocity), 0, 1) * weightedPerlinNoise(stepSin, 0.3f, Time.time, 30f, 0.5f) * Mathf.Clamp((1 - aimVal) * (1 - aimVal), sweyWhileAim, 1f);
|
||||
|
||||
positionMod[1] = new Vector3(deltaX, deltaY, deltaZ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*-----Helper Methods-----*/
|
||||
float gravityValue(float curretnValue, float rateOfChange, float maxValue, float minValue, bool add)
|
||||
{
|
||||
// The currentValue will be advanced or reduced by the rateOfChange depending on the add boolean. But only in the specified range.
|
||||
// Usage: val = gravityValue(val, 0.01f, 1, 0, true);
|
||||
float value = curretnValue;
|
||||
if (add) value += rateOfChange;
|
||||
else value -= rateOfChange;
|
||||
|
||||
return Mathf.Clamp(value, minValue, maxValue);
|
||||
}
|
||||
float weightedPerlinNoise(float value, float weight, float pX, float pY, float offset = 0f)
|
||||
{
|
||||
return value * (1f - weight) + value * weight * (Mathf.PerlinNoise(pX, pY) + offset);
|
||||
}
|
||||
|
||||
float gravityValue(float curretnValue, float rateOfChangePos, float rateOfChangeNeg, float maxValue, float minValue, bool add)
|
||||
{
|
||||
// The currentValue will be advanced by the rateOfChangePos and reduced by the rateOfChangeNeg depending on the add boolean. But only in the specified range.
|
||||
// Usage: val = gravityValue(val, 0.01f, 0.05f, 1, 0, true);
|
||||
float value = curretnValue;
|
||||
if (add) value += rateOfChangePos;
|
||||
else value -= rateOfChangeNeg;
|
||||
|
||||
return Mathf.Clamp(value, minValue, maxValue);
|
||||
}
|
||||
|
||||
private float ezSin(float amplitude, float frequency, float x)
|
||||
{
|
||||
// Simplification of the sin function.
|
||||
return amplitude * Mathf.Sin((x / 3.1831f) * 10 * frequency);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/ProcedualAnimationController.cs.meta
Normal file
11
Assets/Scripts/Player/ProcedualAnimationController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9bd09d0992070b049b461b56fd022956
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user