mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
Basic Recoil
This commit is contained in:
@@ -102,7 +102,7 @@ public class PlayerController : NetworkBehaviour
|
||||
|
||||
if (Input.GetAxisRaw("Sprint") > 0 && isGrounded)
|
||||
{
|
||||
Debug.Log("Sprint");
|
||||
//Debug.Log("Sprint");
|
||||
movementSpeed = sprintSpeed;
|
||||
isSprinting = true;
|
||||
}
|
||||
@@ -115,11 +115,11 @@ public class PlayerController : NetworkBehaviour
|
||||
//Grounded
|
||||
if (velocityY < 0)
|
||||
{
|
||||
velocityY += gravity * Time.deltaTime;
|
||||
velocityY += gravity * 0.9f * Time.deltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
velocityY += gravity * 1.0f * Time.deltaTime;
|
||||
velocityY += gravity* Time.deltaTime;
|
||||
}
|
||||
if (isGrounded && velocityY < 0)
|
||||
velocityY = 0.0f;
|
||||
|
||||
@@ -8,6 +8,7 @@ public class Shoot : NetworkBehaviour
|
||||
[SerializeField] GameObject muzzle;
|
||||
[SerializeField] ShootAnimation shootAnim;
|
||||
[SerializeField] float fireRate;
|
||||
[SerializeField] GameObject gunHoldPos;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -38,6 +39,7 @@ public class Shoot : NetworkBehaviour
|
||||
|
||||
shootAnimation();
|
||||
|
||||
|
||||
if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit))
|
||||
{
|
||||
if (hit.transform.gameObject.GetComponent<Player>() != null)
|
||||
@@ -53,7 +55,7 @@ public class Shoot : NetworkBehaviour
|
||||
// This code will be executed on the Client.
|
||||
void shootAnimation()
|
||||
{
|
||||
shootAnim.StartShootAnimation(fireRate);
|
||||
|
||||
//shootAnim.StartShootAnimation(fireRate);
|
||||
shootAnim.recoil(gunHoldPos,0.1f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,22 +5,61 @@ using UnityEngine;
|
||||
public class ShootAnimation : MonoBehaviour
|
||||
{
|
||||
private Animator anim;
|
||||
[SerializeField]private GameObject gun;
|
||||
[SerializeField] private GameObject gun;
|
||||
private Transform startTransform;
|
||||
Vector3 startPos;
|
||||
Vector3 startRot;
|
||||
[SerializeField] float zOffset = 0f;
|
||||
[SerializeField] float returnForce = 0.06f;
|
||||
[SerializeField] float impulsForce = 0.2f;
|
||||
[SerializeField] float maxRecoil = 0.5f;
|
||||
private int recoilCounter = 0;
|
||||
|
||||
float zVelocity = 0f;
|
||||
|
||||
|
||||
public void OnSwitchWeapon(float fireRate)
|
||||
{
|
||||
//gun = newGun;
|
||||
anim = gun.GetComponent<Animator>();
|
||||
anim.SetFloat("ShootSpeed",1f/(60f/fireRate));
|
||||
startPos = gun.transform.localPosition;
|
||||
startRot = gun.transform.localRotation.eulerAngles;
|
||||
|
||||
}
|
||||
public void StartShootAnimation(float timeInSeconds)
|
||||
|
||||
|
||||
public void recoil(GameObject gun, float force)
|
||||
{
|
||||
Debug.Log(anim.GetFloat("ShootSpeed"));
|
||||
anim.Play("Shoot");
|
||||
recoilCounter++;
|
||||
}
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
|
||||
//Apply recoil based on the number of shots fired
|
||||
for (int i = 0; i < recoilCounter; i++)
|
||||
{
|
||||
zVelocity -= impulsForce * 0.9f + impulsForce * 0.1f * Mathf.PerlinNoise(i,1f);
|
||||
}
|
||||
recoilCounter = 0;
|
||||
|
||||
|
||||
zOffset += zVelocity;
|
||||
|
||||
if (zOffset > 0)
|
||||
{
|
||||
zOffset = 0f;
|
||||
zVelocity = 0f;
|
||||
}
|
||||
else if (zOffset < 0)
|
||||
{
|
||||
zVelocity += returnForce * 0.9f + returnForce * 0.1f * Mathf.PerlinNoise(Time.time,1f);
|
||||
|
||||
}
|
||||
zOffset = Mathf.Clamp(zOffset,-maxRecoil * 0.5f + -maxRecoil * 0.5f * Mathf.PerlinNoise(Time.time * 1000,1),0);
|
||||
gun.transform.localPosition = startPos + new Vector3(0,0,zOffset);
|
||||
gun.transform.localRotation = Quaternion.Euler(startRot.x,startRot.y,startRot.z + zOffset * 50f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user