Camera recoil

This commit is contained in:
juliuse98
2021-11-08 22:56:15 +01:00
parent df60a95c80
commit ea8394bd16
10 changed files with 368 additions and 216 deletions

View File

@@ -5,7 +5,7 @@ using UnityEngine;
public class AimDownSights : MonoBehaviour
{
[SerializeField] float aimSpeed = 0.01f;
[SerializeField][Range(0,1)] float aimVal = 0;
[SerializeField][Range(0,1)] public float aimVal = 0;
[SerializeField] private GameObject gun;
[SerializeField] GameObject AimPoint;
[SerializeField] GameObject HoldPoint;
@@ -15,9 +15,8 @@ public class AimDownSights : MonoBehaviour
return true;
}
private void Update()
private void FixedUpdate()
{
Debug.Log(Input.GetButton("Aim"));
if (Input.GetButton("Aim"))
{
isAiming = true;
@@ -29,7 +28,7 @@ public class AimDownSights : MonoBehaviour
aimVal -= aimSpeed;
}
aimVal = Mathf.Clamp(aimVal,0,1);
gun.transform.position = Vector3.Lerp(HoldPoint.transform.position, AimPoint.transform.position, aimVal);
gun.transform.position = Vector3.Lerp(HoldPoint.transform.position, AimPoint.transform.position,Mathf.Pow(aimVal,1.3f)) ;
}
}

View File

@@ -5,6 +5,7 @@ using Mirror;
public class Headbob : NetworkBehaviour
{
[SerializeField] private PlayerController playerController;
[SerializeField] private ShootAnimation gunAnimation;
[SerializeField] private float posCheckDistance = 0.01f;
@@ -22,6 +23,14 @@ public class Headbob : NetworkBehaviour
private Vector3 newPos;
private float oldDist = 0;
private float lerpVal = 0;
[Header("Gun Settings")]
[SerializeField] GameObject gunRotation;
[SerializeField] float rotationMultiplier = 0.1f;
private void Start()
{
lastPos = this.transform.position;
@@ -50,6 +59,7 @@ public class Headbob : NetworkBehaviour
{
checkDist = currentDist + dist;
}
gunAnimation.gunSideSwey(getSin(amplitude, stepFrequency/2, checkDist),playerController.inputDirection.magnitude);
newPos = new Vector3(getSin(amplitude / 2, stepFrequency, checkDist), getSin(amplitude, stepFrequency, checkDist), 0);
Neck.localPosition = newPos;
}

View File

@@ -8,15 +8,16 @@ using Mirror;
public class PlayerController : NetworkBehaviour
{
[SerializeField] private AimDownSights ADSContoller;
[Header("Movement")]
[SerializeField] private float walkSpeed = 6.0f;
[SerializeField] private float sprintSpeed = 10.0f;
[SerializeField] private float aimWalkSpeed = 3.0f;
[SerializeField][Range(0.0f, 0.5f)] private float moveSmoothTime = 0.001f;
[SerializeField] float gravity = -10.0f;
[SerializeField] private float jumpHeight;
private Vector3 inputDirection = Vector3.zero;
public Vector3 inputDirection = Vector3.zero;
private Vector3 moveDirection;
[Header("Ground Check")]
@@ -100,15 +101,15 @@ public class PlayerController : NetworkBehaviour
{
if (Input.GetAxisRaw("Sprint") > 0 && isGrounded)
if (Input.GetAxisRaw("Sprint") > 0 && isGrounded && !ADSContoller.isAiming)
{
//Debug.Log("Sprint");
movementSpeed = sprintSpeed;
isSprinting = true;
}
else
{
movementSpeed = walkSpeed;
if(ADSContoller.isAiming) movementSpeed = aimWalkSpeed;
else movementSpeed = walkSpeed;
isSprinting = false;
}

View File

@@ -18,7 +18,7 @@ public class PlayerMouseLook : NetworkBehaviour
[SerializeField] [Range(0.0f, 0.5f)] private float mouseSmoothTime = 0.001f;
[SerializeField] private bool lockCursor = true;
private float fullPitch = 0f;
public float fullPitch = 0f;
private float cameraPitch = 0f;
private float neckPitch = 0f;
private float velocityY = 0.0f;

View File

@@ -22,13 +22,26 @@ public class ShootAnimation : MonoBehaviour
[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] AimDownSights ADSController;
[SerializeField] bool sideSwey = true;
[SerializeField] float sweyMult = 15f;
[SerializeField] float sweyWhileAim = 0.1f;
float swey = 0f;
[SerializeField] float returnForce = 0.006f;
@@ -59,11 +72,20 @@ public class ShootAnimation : MonoBehaviour
{
//Play the animation
anim.Play("Shoot");
playerMouseLook.fullPitch -= cameraRecoilX * Mathf.PerlinNoise(Time.time * 3f + 10f, 1f);
transform.Rotate(Vector3.up * ((Mathf.PerlinNoise(Time.time * 1f + 10f, 1f) - 0.5f) * 2f) * cameraRecoilY);
//Add force for the recoil
recoilCounter++;
}
public void gunSideSwey(float sinVal,float moveInput)
{
swey = (sweyMult * sinVal * moveInput * 0.7f
+ sweyMult * sinVal * moveInput * ((Mathf.PerlinNoise(Time.time * 1f + 10f, 1f) - 0.5f) * 2f) * 0.3f) * Mathf.Clamp((1 - ADSController.aimVal) * (1 - ADSController.aimVal), sweyWhileAim,1f);
}
void FixedUpdate()
{
@@ -93,9 +115,12 @@ public class ShootAnimation : MonoBehaviour
//Position recoil
if (positionRecoil)
{
int sideLock = 0;
if (sideSwey) sideLock = 1;
gunPositionObj.transform.localPosition = startPos + new Vector3(
positionMultX * zOffset * ((Mathf.PerlinNoise(Time.time * 1f + 10f, 1f) - 0.5f) * 2f),
positionMultY * zOffset * ((Mathf.PerlinNoise(Time.time * 2f + 20f, 2f) - 0.5f) * 2f),
positionMultX * zOffset * ((Mathf.PerlinNoise(Time.time * 1f + 10f, 1f) - 0.5f) * 2f) + sideLock * swey,
positionMultY * zOffset * Mathf.PerlinNoise(Time.time * 2f + 20f, 2f),
positionMultZ* zOffset * ((Mathf.PerlinNoise(Time.time * 3f + 30f, 3f) - 0.5f) * 2f));
}
else
@@ -109,13 +134,15 @@ public class ShootAnimation : MonoBehaviour
int xLock = 0;
int yLock = 0;
int zLock = 0;
if (rotX) xLock = 1;
if (rotY) yLock = 1;
if (rotZ) zLock = 1;
gunRotationObj.transform.localRotation = Quaternion.Euler(
startRot.x + xLock * rotationMultX * zOffset * ((Mathf.PerlinNoise(Time.time * 3f + 30f, 4f) - 0.5f) * 2f),
startRot.x + xLock * rotationMultX * zOffset * Mathf.PerlinNoise(Time.time * 3f + 30f, 4f),
startRot.y + yLock * rotationMultY * zOffset * ((Mathf.PerlinNoise(Time.time * 2f + 10f, 3f) - 0.5f) * 2f),
startRot.z + zLock * rotationMultZ * zOffset * ((Mathf.PerlinNoise(Time.time * 1.5f, 2f) - 0.5f) * 2f));
}