Advertisement
DugganSC

Untitled

Apr 2nd, 2025
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class HitscanTurret : MonoBehaviour
  7. {
  8.     [SerializeField] float _attackRange = 1f;
  9.     [SerializeField] float _damagePerSecond = 10f;
  10.     [SerializeField] float _turnSpeed = 1f;
  11.     [SerializeField] float _targetingArc = 5f;
  12.  
  13.     [SerializeField] GameObject _turretObject;
  14.  
  15.     [SerializeField] ParticleSystem _muzzleFlash;
  16.    
  17.     [SerializeField] GameObject _closestEnemy;
  18.  
  19.     private void OnDrawGizmos()
  20.     {
  21.         Gizmos.DrawWireSphere(transform.position, _attackRange);
  22.     }
  23.  
  24.     // Update is called once per frame
  25.     void Update()
  26.     {
  27.         FindEnemy();
  28.  
  29.         AimAtAndShootAtCurrentEnemy();
  30.     }
  31.  
  32.     private void AimAtAndShootAtCurrentEnemy()
  33.     {
  34.         if (_closestEnemy == null)
  35.         {
  36.             return;
  37.         }
  38.  
  39.         float distance = Vector3.Distance(transform.position, _closestEnemy.transform.position);
  40.         if ( distance > _attackRange)
  41.         {
  42.             Debug.Log("Lost sight of " + _closestEnemy.name);
  43.             _closestEnemy = null;
  44.             return;
  45.         }
  46.  
  47.         Vector3 targetVector = _closestEnemy.transform.position - transform.position;
  48.         // Rotate toward the hit
  49.         Debug.Log("Rotating toward " + _closestEnemy.name);
  50.         _turretObject.transform.forward = Vector3.RotateTowards(transform.forward, targetVector, _turnSpeed * Time.deltaTime, 0f);
  51.  
  52.         // If in target arc, fire
  53.         if (Vector3.Angle(transform.forward, targetVector) < _targetingArc)
  54.         {
  55.             // _muzzleFlash.Play();
  56.             Debug.Log("Shooting for " + _damagePerSecond * Time.deltaTime);
  57.             // Play the animation for firing
  58.             if (_closestEnemy.TryGetComponent<Damageable>(out Damageable target))
  59.             {
  60.                 target.Damage(_damagePerSecond * Time.deltaTime);
  61.             }
  62.         }
  63.     }
  64.  
  65.     private void FindEnemy()
  66.     {
  67.         if (_closestEnemy != null)
  68.         {
  69.             return;
  70.         }
  71.        
  72.         Collider[] contacts = Physics.OverlapSphere(transform.position, _attackRange, LayerMask.GetMask("Enemy"));
  73.  
  74.         if (contacts.Length < 1)
  75.         {
  76.             return;
  77.         }
  78.  
  79.         _closestEnemy = contacts[0].gameObject;
  80.         float _closestDistance = Vector3.Distance(transform.position, _closestEnemy.transform.position);
  81.  
  82.         for (int i = 1; i < contacts.Length; i++)
  83.         {
  84.             float distance = Vector3.Distance(transform.position, contacts[i].gameObject.transform.position);
  85.             if (distance < _closestDistance) {
  86.                 _closestDistance = distance;
  87.                 _closestEnemy = contacts[i].gameObject;
  88.             }
  89.         }
  90.  
  91.  
  92.        
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement