ETFXFireProjectile.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using System.Collections;
  4. namespace EpicToonFX
  5. {
  6. public class ETFXFireProjectile : MonoBehaviour
  7. {
  8. [SerializeField]
  9. public GameObject[] projectiles;
  10. [Header("Missile spawns at attached game object")]
  11. public Transform spawnPosition;
  12. [HideInInspector]
  13. public int currentProjectile = 0;
  14. public float speed = 500;
  15. // MyGUI _GUI;
  16. ETFXButtonScript selectedProjectileButton;
  17. void Start()
  18. {
  19. selectedProjectileButton = GameObject.Find("Button").GetComponent<ETFXButtonScript>();
  20. }
  21. RaycastHit hit;
  22. void Update()
  23. {
  24. if (Input.GetKeyDown(KeyCode.RightArrow))
  25. {
  26. nextEffect();
  27. }
  28. if (Input.GetKeyDown(KeyCode.D))
  29. {
  30. nextEffect();
  31. }
  32. if (Input.GetKeyDown(KeyCode.A))
  33. {
  34. previousEffect();
  35. }
  36. else if (Input.GetKeyDown(KeyCode.LeftArrow))
  37. {
  38. previousEffect();
  39. }
  40. if (Input.GetKeyDown(KeyCode.Mouse0)) //On left mouse down-click
  41. {
  42. if (!EventSystem.current.IsPointerOverGameObject()) //Checks if the mouse is not over a UI part
  43. {
  44. if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100f)) //Finds the point where you click with the mouse
  45. {
  46. GameObject projectile = Instantiate(projectiles[currentProjectile], spawnPosition.position, Quaternion.identity) as GameObject; //Spawns the selected projectile
  47. projectile.transform.LookAt(hit.point); //Sets the projectiles rotation to look at the point clicked
  48. projectile.GetComponent<Rigidbody>().AddForce(projectile.transform.forward * speed); //Set the speed of the projectile by applying force to the rigidbody
  49. }
  50. }
  51. }
  52. Debug.DrawRay(Camera.main.ScreenPointToRay(Input.mousePosition).origin, Camera.main.ScreenPointToRay(Input.mousePosition).direction * 100, Color.yellow);
  53. }
  54. public void nextEffect() //Changes the selected projectile to the next. Used by UI
  55. {
  56. if (currentProjectile < projectiles.Length - 1)
  57. currentProjectile++;
  58. else
  59. currentProjectile = 0;
  60. selectedProjectileButton.getProjectileNames();
  61. }
  62. public void previousEffect() //Changes selected projectile to the previous. Used by UI
  63. {
  64. if (currentProjectile > 0)
  65. currentProjectile--;
  66. else
  67. currentProjectile = projectiles.Length - 1;
  68. selectedProjectileButton.getProjectileNames();
  69. }
  70. public void AdjustSpeed(float newSpeed) //Used by UI to set projectile speed
  71. {
  72. speed = newSpeed;
  73. }
  74. }
  75. }