| | | 1 | | using BNG; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using HenryLab; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Apply forward force to instantiated prefab |
| | | 8 | | /// </summary> |
| | | 9 | | public class LaunchProjectile : MonoBehaviour, ITriggerableEntity, IGrabbableEntity |
| | | 10 | | { |
| | | 11 | | [ExcludeFromCodeCoverage] public float TriggeringTime => 1.5f; |
| | | 12 | | [ExcludeFromCodeCoverage] public string Name => Str.Gun; |
| | | 13 | | [ExcludeFromCodeCoverage] public Transform Destination => null; |
| | | 14 | | |
| | | 15 | | [ExcludeFromCodeCoverage] |
| | | 16 | | public Grabbable Grabbable |
| | | 17 | | { |
| | | 18 | | get |
| | | 19 | | { |
| | | 20 | | var g = GetComponent<Grabbable>(); |
| | | 21 | | if(g) return g; |
| | | 22 | | return gameObject.AddComponent<Grabbable>(); |
| | | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | [ExcludeFromCodeCoverage] public void Triggerring() { } |
| | | 27 | | |
| | | 28 | | [ExcludeFromCodeCoverage] |
| | | 29 | | public void Triggerred() |
| | | 30 | | { |
| | | 31 | | Fire(); |
| | | 32 | | } |
| | | 33 | | [ExcludeFromCodeCoverage] |
| | | 34 | | public void OnReleased() |
| | | 35 | | { |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | [ExcludeFromCodeCoverage] public void OnGrabbed() { } |
| | | 39 | | |
| | | 40 | | [Tooltip("The projectile that's created")] |
| | 4 | 41 | | public GameObject projectilePrefab = null; |
| | | 42 | | |
| | | 43 | | [Tooltip("The point that the project is created")] |
| | 4 | 44 | | public Transform startPoint = null; |
| | | 45 | | |
| | | 46 | | [Tooltip("The speed at which the projectile is launched")] |
| | 4 | 47 | | public float launchSpeed = 1.0f; |
| | | 48 | | |
| | | 49 | | public void Fire() |
| | 6 | 50 | | { |
| | 6 | 51 | | GameObject newObject = Instantiate(projectilePrefab, startPoint.position, startPoint.rotation); |
| | | 52 | | |
| | 6 | 53 | | if(newObject.TryGetComponent(out Rigidbody rigidBody)) |
| | 6 | 54 | | ApplyForce(rigidBody); |
| | 6 | 55 | | } |
| | | 56 | | |
| | | 57 | | private void ApplyForce(Rigidbody rigidBody) |
| | 6 | 58 | | { |
| | 6 | 59 | | Vector3 force = startPoint.forward * launchSpeed; |
| | 6 | 60 | | rigidBody.AddForce(force); |
| | 6 | 61 | | } |
| | | 62 | | |
| | | 63 | | } |