From 908af2441f020aa10eefbbf07dc61d0a8b006ec5 Mon Sep 17 00:00:00 2001 From: Noah4ever <66632359+Noah4ever@users.noreply.github.com> Date: Mon, 8 Nov 2021 13:06:53 +0100 Subject: [PATCH] Weapon Pointing + weapon points to raycast from camera --- Assets/Objects/Weapons/WeaponHolder.prefab | 165 ++++++++++++++++++ .../Objects/Weapons/WeaponHolder.prefab.meta | 7 + Assets/Prefabs/Player.prefab | 99 +++++++++-- Assets/Scripts/Weapons/Shoot.cs | 40 +++-- 4 files changed, 280 insertions(+), 31 deletions(-) create mode 100644 Assets/Objects/Weapons/WeaponHolder.prefab create mode 100644 Assets/Objects/Weapons/WeaponHolder.prefab.meta diff --git a/Assets/Objects/Weapons/WeaponHolder.prefab b/Assets/Objects/Weapons/WeaponHolder.prefab new file mode 100644 index 0000000..edde105 --- /dev/null +++ b/Assets/Objects/Weapons/WeaponHolder.prefab @@ -0,0 +1,165 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &2189428797618060492 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 3579399904404681179} + m_Layer: 0 + m_Name: BulletExitPoint + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &3579399904404681179 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2189428797618060492} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 9219943091343242540} + m_RootOrder: 7 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &3102687152649998300 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 9067311473762665769} + m_Layer: 0 + m_Name: WeaponHolder + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &9067311473762665769 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3102687152649998300} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 9219943091343242540} + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &8722617901319436246 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8410474266686189462} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c98f5c47a8b7dd64f86fd6f42c4d6e5e, type: 3} + m_Name: + m_EditorClassIdentifier: + weaponKind: 1 + active: 0 + damage: 15 + firerate: 1 + roundsPerShot: 1 + recoilStrength: 15 + currentAmmunition: 12 + magazinSize: 12 + totalAmmunition: 24 + bulletExit: {fileID: 2189428797618060492} + allowAction: 1 +--- !u!1001 &8681084213692846791 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 9067311473762665769} + m_Modifications: + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_RootOrder + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalScale.x + value: 0.2 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalScale.y + value: 0.2 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalScale.z + value: 0.2 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalPosition.x + value: 0.53 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalPosition.y + value: -0.18 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalPosition.z + value: 0.842 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalRotation.w + value: 0.7071068 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalRotation.y + value: -0.7071068 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: -90 + objectReference: {fileID: 0} + - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 919132149155446097, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + propertyPath: m_Name + value: USP + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} +--- !u!1 &8410474266686189462 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + m_PrefabInstance: {fileID: 8681084213692846791} + m_PrefabAsset: {fileID: 0} +--- !u!4 &9219943091343242540 stripped +Transform: + m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + m_PrefabInstance: {fileID: 8681084213692846791} + m_PrefabAsset: {fileID: 0} diff --git a/Assets/Objects/Weapons/WeaponHolder.prefab.meta b/Assets/Objects/Weapons/WeaponHolder.prefab.meta new file mode 100644 index 0000000..8a593db --- /dev/null +++ b/Assets/Objects/Weapons/WeaponHolder.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 352fda1e84382b540b8dcf5fc13fcfd8 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 7654eb3..22f1e04 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -1,5 +1,36 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &117587211917336539 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 4583300602822713530} + m_Layer: 0 + m_Name: GunPosition + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &4583300602822713530 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 117587211917336539} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 9097354778471689966} + m_Father: {fileID: 1155604412711610141} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &351275810583331071 GameObject: m_ObjectHideFlags: 0 @@ -112,6 +143,37 @@ MeshRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_AdditionalVertexStreams: {fileID: 0} +--- !u!1 &2940254478074645882 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 9097354778471689966} + m_Layer: 0 + m_Name: GunRotation + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &9097354778471689966 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2940254478074645882} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: -0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 9086299259537425155} + m_Father: {fileID: 4583300602822713530} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &3104486771267849676 GameObject: m_ObjectHideFlags: 0 @@ -137,10 +199,10 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 3104486771267849676} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: 0.2703267, y: -0.1790001, z: 0.7789205} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - - {fileID: 9086299259537425155} + - {fileID: 4583300602822713530} m_Father: {fileID: 6272346181657429396} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} @@ -679,6 +741,7 @@ MonoBehaviour: syncInterval: 0.1 muzzle: {fileID: 351275810583331071} weaponHolder: {fileID: 8454054357811321273} + GunRotation: {fileID: 2940254478074645882} --- !u!1 &6272346181657429393 GameObject: m_ObjectHideFlags: 0 @@ -927,7 +990,7 @@ PrefabInstance: m_ObjectHideFlags: 0 serializedVersion: 2 m_Modification: - m_TransformParent: {fileID: 1155604412711610141} + m_TransformParent: {fileID: 9097354778471689966} m_Modifications: - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_RootOrder @@ -935,7 +998,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalScale.x - value: 0.15 + value: 0.15000002 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalScale.y @@ -943,35 +1006,35 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalScale.z - value: 0.15 + value: 0.15000002 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalPosition.x - value: 0.27032673 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalPosition.y - value: -0.17900014 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalPosition.z - value: 0.77892053 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalRotation.w - value: 0.68995017 + value: 0.6952859 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalRotation.x - value: -0 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalRotation.y - value: -0.7238569 + value: -0.7187333 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalRotation.z - value: -0 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalEulerAnglesHint.x @@ -979,7 +1042,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalEulerAnglesHint.y - value: -92.748 + value: -91.9 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} propertyPath: m_LocalEulerAnglesHint.z @@ -991,13 +1054,13 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} ---- !u!4 &9086299259537425155 stripped -Transform: - m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} - m_PrefabInstance: {fileID: 8760552908600304872} - m_PrefabAsset: {fileID: 0} --- !u!1 &8454054357811321273 stripped GameObject: m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} m_PrefabInstance: {fileID: 8760552908600304872} m_PrefabAsset: {fileID: 0} +--- !u!4 &9086299259537425155 stripped +Transform: + m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: db8a9548ba56c1949bb28f7c6129bbeb, type: 3} + m_PrefabInstance: {fileID: 8760552908600304872} + m_PrefabAsset: {fileID: 0} diff --git a/Assets/Scripts/Weapons/Shoot.cs b/Assets/Scripts/Weapons/Shoot.cs index 3337cf5..a273ea2 100644 --- a/Assets/Scripts/Weapons/Shoot.cs +++ b/Assets/Scripts/Weapons/Shoot.cs @@ -6,18 +6,27 @@ using Mirror; public class Shoot : NetworkBehaviour { [SerializeField] GameObject muzzle; + [SerializeField] ShootAnimation shootAnim; + [SerializeField] GameObject gunHoldPos; [SerializeField] GameObject weaponHolder; - private Weapon weapon; + [SerializeField] GameObject GunRotation; + Ammunition ammunition; + private Weapon weapon; private RaycastHit crosshairHitPoint; private Camera mCamera; private Vector3 _pointDirection; private Quaternion _lookRotation; private void Start() { - mCamera = Camera.main; - weapon = weaponHolder.GetComponent(); + + if (isLocalPlayer) + { + mCamera = Camera.main; + weapon = weaponHolder.GetComponent(); + shootAnim.OnSwitchWeapon(weapon.Firerate); + } } private void Update() @@ -50,17 +59,18 @@ public class Shoot : NetworkBehaviour // This code will be executed on the Server. private void CmdFireBullet() { - Physics.Raycast(mCamera.transform.position, mCamera.transform.forward, out crosshairHitPoint); - Debug.DrawLine(mCamera.transform.position, crosshairHitPoint.point); - _pointDirection = crosshairHitPoint.point - muzzle.transform.position; - _lookRotation = Quaternion.LookRotation(_pointDirection); - weapon.transform.rotation = Quaternion.RotateTowards(weapon.transform.rotation, _lookRotation, 1f); - if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out RaycastHit hit) && weapon.CurrentAmmunition > 0) + RaycastHit hit; + Ray ray = new Ray(mCamera.transform.position, mCamera.transform.forward); + Physics.Raycast(ray, out crosshairHitPoint, 5000f); + + if (crosshairHitPoint.distance != 0 && crosshairHitPoint.distance < 2) // Turning Weapon to shooting point { - Debug.DrawLine(muzzle.transform.position, hit.point); + _pointDirection = crosshairHitPoint.point - muzzle.transform.position; + _lookRotation = Quaternion.LookRotation(_pointDirection); + GunRotation.transform.rotation = Quaternion.RotateTowards(GunRotation.transform.rotation, _lookRotation, 1f); } - if (weapon.AllowAction) + if (weapon.AllowAction) // shooting { if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit) && weapon.CurrentAmmunition > 0) { @@ -80,11 +90,15 @@ public class Shoot : NetworkBehaviour } } - + [Client] + void shootAnimation() + { + shootAnim.recoil(gunHoldPos, 0.1f); + } IEnumerator fireRate() { weapon.AllowAction = false; - yield return new WaitForSeconds(weapon.Firerate); + yield return new WaitForSeconds(60f/weapon.Firerate); weapon.AllowAction = true; }