Advertisement
TOTALMM

EnemyShoot

Feb 20th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace Project
  6. {
  7. public class EnemyShoot : MonoBehaviour
  8. {
  9. public float enemyDamage = 1f;
  10. public float enemyRange = 2f;
  11.  
  12. public float shotDelay = 0.2f; //Delay of firing
  13. public float targetStart = 2f; //The time it takes inbetween targetting
  14. public float targetEnd = 0f; //Used for the loop below VVVV
  15.  
  16. Transform player;
  17. Vector3 raycastDirection;
  18. // Use this for initialization
  19. void Start()
  20. {
  21. player = GameObject.FindGameObjectWithTag("Player").transform;
  22. }
  23.  
  24. // Update is called once per frame
  25. // Update loop will have issues in it
  26. void Update()
  27. {
  28. if (!GUIManager.game_stop && GUIManager.gameStart)
  29. {
  30. targetStart -= Time.deltaTime;
  31.  
  32. if (targetStart <= targetEnd) //Should cycle every 2 seconds
  33. {
  34. shotDelay -= Time.deltaTime;
  35. targetStart = 2f;
  36. Target();
  37. if (shotDelay <= 0f)
  38. {
  39. shotDelay = 0.2f;
  40. Shoot();
  41. }
  42. }
  43. }
  44. }
  45. // This is what I am using to Target, so that if the player stands still for too long, they are shot.
  46. void Target()
  47. {
  48. RaycastHit hit;
  49. Debug.Log("Target");
  50. raycastDirection = player.transform.position;
  51. if (Physics.Raycast(gameObject.transform.position, raycastDirection, out hit, enemyRange))
  52. {
  53. Debug.DrawLine(gameObject.transform.position, player.transform.position, Color.green, 10f);
  54. }
  55. }
  56. //This is the actual shot itself, it should be delayed by a certain amount after the player has been targeted.
  57. void Shoot()
  58. {
  59. Debug.Log("Shoot");
  60. if (player.tag == "Player")
  61. {
  62. Debug.Log("Get hit");
  63. Player.TakeDamage(enemyDamage);
  64. }
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement