Added gun shoot event and recoil
This commit is contained in:
51
3D FPS/Assets/Scripts/Gun.cs
Normal file
51
3D FPS/Assets/Scripts/Gun.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class Gun : MonoBehaviour
|
||||
{
|
||||
public UnityEvent onGunShoot;
|
||||
public float fireCoolDown;
|
||||
public bool automatic;
|
||||
|
||||
|
||||
private float currentCooldown;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
currentCooldown = fireCoolDown;
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(automatic)
|
||||
{
|
||||
if(Input.GetMouseButton(0))
|
||||
{
|
||||
if(currentCooldown <= 0f)
|
||||
{
|
||||
//if not null (?)
|
||||
onGunShoot?.Invoke();
|
||||
currentCooldown = fireCoolDown;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Input.GetMouseButtonDown(0))
|
||||
{
|
||||
if(currentCooldown <= 0f)
|
||||
{
|
||||
//if not null (?)
|
||||
onGunShoot?.Invoke();
|
||||
currentCooldown = fireCoolDown;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
currentCooldown -= Time.deltaTime;
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Gun.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Gun.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d4caf6dbac4fca4981c2750e27025ba
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
39
3D FPS/Assets/Scripts/GunRecoil.cs
Normal file
39
3D FPS/Assets/Scripts/GunRecoil.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.PlasticSCM.Editor.WebApi;
|
||||
using UnityEngine;
|
||||
|
||||
public class GunRecoil : MonoBehaviour
|
||||
{
|
||||
public Transform gunTransform;
|
||||
public float recoilDistance = 0.1f;
|
||||
public float recoilRotation = 5f;
|
||||
public float recoilSpeed = 10f;
|
||||
public float recoverySpeed = 5f;
|
||||
|
||||
private Vector3 gunCurrPos;
|
||||
private Vector3 gunOriginalPos;
|
||||
private Quaternion originalRotation;
|
||||
private Vector3 currentRecoilRotation;
|
||||
|
||||
void Start()
|
||||
{
|
||||
gunOriginalPos = gunTransform.localPosition;
|
||||
originalRotation = gunTransform.localRotation;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
gunCurrPos = Vector3.Lerp(gunCurrPos, Vector3.zero, Time.deltaTime * recoverySpeed);
|
||||
currentRecoilRotation = Vector3.Lerp(currentRecoilRotation, Vector3.zero, Time.deltaTime * recoverySpeed);
|
||||
|
||||
gunTransform.localPosition = gunOriginalPos + gunCurrPos;
|
||||
gunTransform.localRotation = originalRotation * Quaternion.Euler(currentRecoilRotation);
|
||||
}
|
||||
|
||||
public void ApplyRecoil()
|
||||
{
|
||||
gunCurrPos += new Vector3(0, 0, -recoilDistance);
|
||||
currentRecoilRotation += new Vector3(-recoilRotation, Random.Range(-recoilRotation / 2, recoilRotation / 2), 0);
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/GunRecoil.cs.meta
Normal file
11
3D FPS/Assets/Scripts/GunRecoil.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0b55e29fa6ce2a468749b639367d7dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -10,6 +10,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
public float gravity = -9.81f;
|
||||
public float jumpHeight = 3f;
|
||||
|
||||
|
||||
public Transform groundCheck;
|
||||
public float groundDistance = 0.4f;
|
||||
public LayerMask groundMask;
|
||||
|
||||
Reference in New Issue
Block a user