Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Gun : MonoBehaviour {
  6.  
  7. public float damage = 10f;
  8. public float range = 100f;
  9.  
  10. public Transform gunEnd;
  11.  
  12. [SerializeField]
  13. LayerMask mask;
  14.  
  15. public ParticleSystem muzzleFlash;
  16.  
  17. public LineRenderer gunRay;
  18.  
  19. public float accuracy = 1f;
  20.  
  21. float randomOffset_x;
  22. float randomOffset_y;
  23. float randomOffset_z;
  24.  
  25. public GameObject gun;
  26.  
  27. void Start(){
  28. gunRay.enabled = false;
  29. }
  30.  
  31. void Update () {
  32. RaycastHit gunrayhit;
  33. Ray gunlookat = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2 ));
  34. if (Physics.Raycast (gunlookat, out gunrayhit, range)) {
  35. gun.transform.LookAt (gunrayhit.point);
  36. }
  37.  
  38. if (Input.GetButtonDown ("Fire1")) {
  39. Shoot ();
  40. }
  41. }
  42.  
  43. void Shoot(){
  44.  
  45. randomOffset_x = UnityEngine.Random.Range(0, 1 - accuracy);
  46. randomOffset_y = UnityEngine.Random.Range(0, 1 - accuracy);
  47. randomOffset_z = UnityEngine.Random.Range(0, 1 -accuracy);
  48.  
  49. Vector3 direction = transform.forward;
  50.  
  51. direction.x += randomOffset_x;
  52. direction.y += randomOffset_y;
  53. direction.z += randomOffset_z;
  54.  
  55.  
  56. muzzleFlash.Play ();
  57.  
  58. RaycastHit cameraHit;
  59.  
  60. Ray cameraAim = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2 ));
  61.  
  62. cameraAim.direction = direction;
  63.  
  64. if (Physics.Raycast (cameraAim, out cameraHit, range, mask)) {
  65. Debug.DrawLine (cameraAim.origin, cameraHit.point, Color.green);
  66.  
  67. Vector3 cameraHitPoint = cameraHit.point;
  68. float cameraDistance = cameraHit.distance ;
  69.  
  70.  
  71. if (Physics.Raycast (cameraAim, out cameraHit)) {
  72.  
  73. RaycastHit playerHit;
  74.  
  75. Ray ray = new Ray (gunEnd.transform.position, cameraHitPoint - gunEnd.transform.position);
  76.  
  77. if (Physics.Raycast (ray, out playerHit, range)) {
  78.  
  79. Debug.DrawRay (gunEnd.transform.position, cameraHitPoint - gunEnd.transform.position, Color.red);
  80.  
  81. float playerDistance = playerHit.distance;
  82.  
  83. Target target = playerHit.transform.GetComponent<Target> ();
  84. if (target != null) {
  85. target.TakeDamage (damage);
  86. }
  87. StartCoroutine (gunHit (playerHit));
  88. }
  89. }
  90. } else {
  91. Debug.DrawLine (cameraAim.origin, cameraAim.origin + cameraAim.direction * range, Color.green);
  92.  
  93. StartCoroutine (gunMiss(cameraAim));
  94. }
  95.  
  96. }
  97. IEnumerator gunHit(RaycastHit hit){
  98. gunRay.enabled = true;
  99. gunRay.SetPosition (0, gunEnd.position);
  100. gunRay.SetPosition (1, hit.point);
  101. yield return null;
  102. gunRay.enabled = false;
  103. }
  104. IEnumerator gunMiss(Ray ray){
  105. gunRay.enabled = true;
  106. gunRay.SetPosition (0, gunEnd.position);
  107. gunRay.SetPosition(1, ray.origin + ray.direction * range);
  108. yield return null;
  109. gunRay.enabled = false;
  110.  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement