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

@@ -286,7 +286,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 4058555163434875561}
m_RootOrder: 2
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: -90, y: 90, z: 0}
--- !u!1 &2221492823855642591
GameObject:
@@ -317,7 +317,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 4058555163434875561}
m_RootOrder: 3
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: -90, y: 90, z: 0}
--- !u!1 &2992488853448454767
GameObject:
@@ -343,7 +343,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2992488853448454767}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0.5}
m_LocalPosition: {x: 0, y: 0, z: 0.35}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
@@ -680,11 +680,14 @@ MonoBehaviour:
m_EditorClassIdentifier:
syncMode: 0
syncInterval: 0.1
ADSContoller: {fileID: 7198056240135858432}
walkSpeed: 6
sprintSpeed: 8
aimWalkSpeed: 3
moveSmoothTime: 0.05
gravity: -10
jumpHeight: 1.2
inputDirection: {x: 0, y: 0, z: 0}
groundCheck: {fileID: 6272346182417644039}
groundMask:
serializedVersion: 2
@@ -760,6 +763,7 @@ MonoBehaviour:
minCameraAngle: -90
mouseSmoothTime: 0.001
lockCursor: 1
fullPitch: 0
--- !u!114 &8436279266335313624
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -794,12 +798,15 @@ MonoBehaviour:
syncMode: 0
syncInterval: 0.1
playerController: {fileID: -4574526707603160507}
gunAnimation: {fileID: 4276885029697240453}
posCheckDistance: 1
checkDist: 0
stepAmplitudeWalking: 0.05
stepAmplitudeSprinting: 0.07
stepFrequency: 0.8
Neck: {fileID: 6272346181657429396}
gunRotation: {fileID: 0}
rotationMultiplier: 0.1
--- !u!114 &3781802212159559521
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -932,18 +939,26 @@ MonoBehaviour:
gunRotationObj: {fileID: 7768205653369755348}
positionRecoil: 1
rotationRecoil: 1
positionMultX: 0.1
positionMultY: 0.1
positionMultX: 0.2
positionMultY: 0.5
positionMultZ: 1
playerMouseLook: {fileID: 5630859218361338035}
cameraRecoilX: 8
cameraRecoilY: 4
rotX: 1
rotationMultX: 25
rotationMultX: 60
rotationOffsetX: 0.1
rotY: 1
rotationMultY: 25
rotationMultY: 50
rotZ: 1
rotationMultZ: 15
rotationMultZ: 100
ADSController: {fileID: 7198056240135858432}
sideSwey: 1
sweyMult: 1.2
sweyWhileAim: 0.05
returnForce: 0.006
impulsForce: 0.025
maxRecoil: 0.1
maxRecoil: 0.25
--- !u!114 &7198056240135858432
MonoBehaviour:
m_ObjectHideFlags: 0
@@ -956,7 +971,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 916f2d37f60a91149bbca3280a1b69ec, type: 3}
m_Name:
m_EditorClassIdentifier:
aimSpeed: 0.03
aimSpeed: 0.09
aimVal: 1
gun: {fileID: 6522444664664986769}
AimPoint: {fileID: 2992488853448454767}
@@ -1110,7 +1125,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6522444664664986769}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0.5}
m_LocalPosition: {x: 0, y: 0, z: 0.4}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
@@ -1221,7 +1236,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7263663517583319679}
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.04}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
@@ -1310,7 +1325,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8043317747957554736}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0.134, y: -0.115, z: 0.397}
m_LocalPosition: {x: 0.108, y: -0.084, z: 0.35}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
@@ -1439,10 +1454,106 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: -6617984589164989631, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.w
value: 0.49565774
objectReference: {fileID: 0}
- target: {fileID: -6617984589164989631, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.x
value: 0.4028321
objectReference: {fileID: 0}
- target: {fileID: -6617984589164989631, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.y
value: 0.47084302
objectReference: {fileID: 0}
- target: {fileID: -6617984589164989631, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.z
value: 0.6085693
objectReference: {fileID: 0}
- target: {fileID: -5945183352512053224, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.w
value: 0.5662176
objectReference: {fileID: 0}
- target: {fileID: -5945183352512053224, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.x
value: 0.8008484
objectReference: {fileID: 0}
- target: {fileID: -5945183352512053224, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.y
value: 0.04959982
objectReference: {fileID: 0}
- target: {fileID: -5945183352512053224, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.z
value: -0.18862498
objectReference: {fileID: 0}
- target: {fileID: -5815427104119794202, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.w
value: 0.6432096
objectReference: {fileID: 0}
- target: {fileID: -5815427104119794202, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.x
value: -0.7010284
objectReference: {fileID: 0}
- target: {fileID: -5815427104119794202, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.y
value: 0.02918151
objectReference: {fileID: 0}
- target: {fileID: -5815427104119794202, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.z
value: 0.30657643
objectReference: {fileID: 0}
- target: {fileID: -4598867093515939143, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.w
value: 0.64756745
objectReference: {fileID: 0}
- target: {fileID: -4598867093515939143, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.x
value: -0.6947033
objectReference: {fileID: 0}
- target: {fileID: -4598867093515939143, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.y
value: -0.034390952
objectReference: {fileID: 0}
- target: {fileID: -4598867093515939143, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.z
value: -0.3112249
objectReference: {fileID: 0}
- target: {fileID: 919132149155446097, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_Name
value: Human without head
objectReference: {fileID: 0}
- target: {fileID: 1740614089614901821, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.w
value: 0.556481
objectReference: {fileID: 0}
- target: {fileID: 1740614089614901821, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.x
value: 0.80733955
objectReference: {fileID: 0}
- target: {fileID: 1740614089614901821, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.y
value: -0.048747048
objectReference: {fileID: 0}
- target: {fileID: 1740614089614901821, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.z
value: 0.19014606
objectReference: {fileID: 0}
- target: {fileID: 2965664082145358743, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.w
value: 0.4882959
objectReference: {fileID: 0}
- target: {fileID: 2965664082145358743, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.x
value: 0.41018188
objectReference: {fileID: 0}
- target: {fileID: 2965664082145358743, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.y
value: -0.4844513
objectReference: {fileID: 0}
- target: {fileID: 2965664082145358743, guid: 01316cb596562384da845bc519fb90de, type: 3}
propertyPath: m_LocalRotation.z
value: -0.59885305
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 01316cb596562384da845bc519fb90de, type: 3}
--- !u!1 &6576156620079053231 stripped

View File

@@ -302,6 +302,10 @@ PrefabInstance:
propertyPath: m_Name
value: DebugCanvas
objectReference: {fileID: 0}
- target: {fileID: 4636852677861728911, guid: e4f2c82228df5bc4f8fc674e98c7275b, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: e4f2c82228df5bc4f8fc674e98c7275b, type: 3}
--- !u!1 &518996702

View File

@@ -1041,7 +1041,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 4636852677861728911, guid: e4f2c82228df5bc4f8fc674e98c7275b, type: 3}
propertyPath: m_IsActive
value: 1
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4636852677968575729, guid: e4f2c82228df5bc4f8fc674e98c7275b, type: 3}
propertyPath: m_text

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));
}