mirror of
				https://github.com/DerTyp7/defrain-shooter-unity.git
				synced 2025-10-29 20:52:10 +01:00 
			
		
		
		
	Full Recoil animation
This commit is contained in:
		| @@ -23,8 +23,6 @@ public class Shoot : NetworkBehaviour | ||||
|         { | ||||
|             if (Input.GetKeyDown(KeyCode.Mouse0)) | ||||
|             { | ||||
|                 //CmdFireBullet(); | ||||
|                 //RpcOnFire(); | ||||
|                 CmdFireBullet(); | ||||
|             } | ||||
|         } | ||||
| @@ -55,7 +53,6 @@ public class Shoot : NetworkBehaviour | ||||
|     // This code will be executed on the Client. | ||||
|     void shootAnimation() | ||||
|     { | ||||
|        //shootAnim.StartShootAnimation(fireRate); | ||||
|         shootAnim.recoil(gunHoldPos,0.1f); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -4,34 +4,63 @@ using UnityEngine; | ||||
|  | ||||
| public class ShootAnimation : MonoBehaviour | ||||
| { | ||||
|     private Animator anim; | ||||
|     [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; | ||||
|  | ||||
|     [Header("GameObjects")] | ||||
|     [SerializeField] private GameObject gun; | ||||
|     [SerializeField] private GameObject gunHolder; | ||||
|     [SerializeField] private GameObject gunPositionObj; | ||||
|     [SerializeField] private GameObject gunRotationObj; | ||||
|      | ||||
|  | ||||
|     [Header("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] bool rotX = true; | ||||
|     [SerializeField] float rotationMultX = 25f; | ||||
|     [SerializeField] bool rotY = true; | ||||
|     [SerializeField] float rotationMultY = 25f; | ||||
|     [SerializeField] bool rotZ = true; | ||||
|     [SerializeField] float rotationMultZ = 15f; | ||||
|  | ||||
|  | ||||
|  | ||||
|     [SerializeField] float returnForce = 0.006f; | ||||
|     [SerializeField] float impulsForce = 0.025f; | ||||
|     [SerializeField] float maxRecoil = 0.1f; | ||||
|  | ||||
|     private Animator anim; | ||||
|  | ||||
|     Vector3 startPos,startRot; | ||||
|  | ||||
|     float zOffset = 0f; | ||||
|     float zVelocity = 0f; | ||||
|  | ||||
|     int recoilCounter = 0; | ||||
|  | ||||
|     //Has to be called once at the beginning and then again when switching guns | ||||
|     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; | ||||
|  | ||||
|         startPos = gunPositionObj.transform.localPosition; | ||||
|         startRot = gunRotationObj.transform.localRotation.eulerAngles; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public void recoil(GameObject gun, float force) | ||||
|     { | ||||
|         //Play the animation | ||||
|         anim.Play("Shoot"); | ||||
|  | ||||
|         //Add force for the recoil | ||||
|         recoilCounter++; | ||||
|     } | ||||
|  | ||||
| @@ -58,8 +87,43 @@ public class ShootAnimation : MonoBehaviour | ||||
|             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); | ||||
|  | ||||
|         zOffset = Mathf.Clamp(zOffset,-maxRecoil * 0.5f + -maxRecoil * 0.5f * Mathf.PerlinNoise(Time.time * 1000,1),0); | ||||
|  | ||||
|         //Position recoil | ||||
|         if (positionRecoil) | ||||
|         { | ||||
|             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), | ||||
|                 positionMultZ* zOffset * ((Mathf.PerlinNoise(Time.time * 3f + 30f, 3f) - 0.5f) * 2f)); | ||||
|         } | ||||
|         else  | ||||
|         { | ||||
|             gunPositionObj.transform.localPosition = startPos; | ||||
|         } | ||||
|  | ||||
|         //Rotation recoil | ||||
|         if (rotationRecoil) | ||||
|         { | ||||
|             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.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)); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             gunRotationObj.transform.localRotation = Quaternion.Euler(startRot.x, startRot.y, startRot.z); | ||||
|         } | ||||
|              | ||||
|  | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 juliuse98
					juliuse98