Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Eye Script

Discussion in 'Scripting' started by Nails-of-Scream, Apr 5, 2020.

  1. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    I'm working on a multi-entity detection script for my game, or had been for some time ago. I would want someone to look at it and tell me how to improve it and how to fix it. It doesn't add anything to the detected list which is why I'm asking for help. Should I use a for loop?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace EightDirectionalSpriteSystem
    6. {
    7.     public class CharacterEyes : MonoBehaviour
    8.     {
    9.  
    10.         public Vector3 lastSight;
    11.         public List<Entity> detected;
    12.  
    13.         public List<Entity> targetEntity;
    14.         public List<Entity> enemyEntities;
    15.  
    16.         private SphereCollider coli;
    17.         Entity selfActor;
    18.  
    19.         public float fov = 110f;
    20.         Vector3 direction;
    21.         public float angle;
    22.  
    23.         int layerMask;
    24.         RaycastHit hit;
    25.  
    26.         // Start is called before the first frame update
    27.         void Awake()
    28.         {
    29.             selfActor = GetComponentInParent<Entity>();
    30.             layerMask = LayerMask.GetMask("Detectable");
    31.             coli = GetComponent<SphereCollider>();
    32.         }
    33.  
    34.         // Update is called once per frame
    35.         void Update()
    36.         {
    37.             for (int i = detected.Count - 1; i > -1; i--)
    38.             {
    39.                 if (detected[i] == null)
    40.                     detected.RemoveAt(i);
    41.             }
    42.             for (int i = targetEntity.Count - 1; i > -1; i--)
    43.             {
    44.                 if (targetEntity[i] == null)
    45.                     targetEntity.RemoveAt(i);
    46.             }
    47.             for (int i = enemyEntities.Count - 1; i > -1; i--)
    48.             {
    49.                 if (enemyEntities[i] == null)
    50.                     enemyEntities.RemoveAt(i);
    51.             }
    52.         }
    53.  
    54.         private void OnTriggerStay(Collider other)
    55.         {
    56.             if (!targetEntity.Contains(other.gameObject.GetComponent<Entity>())) targetEntity.Add(other.gameObject.GetComponent<Entity>());
    57.             foreach (Entity carry in targetEntity)
    58.             {
    59.                 if (carry != null) direction = carry.transform.position - transform.position;
    60.  
    61.                 angle = Vector3.Angle(direction, transform.forward);
    62.                 if (angle < fov * 0.5f)
    63.                 {
    64.  
    65.                     if (Physics.Raycast(transform.position, direction.normalized, out hit, coli.radius, layerMask, QueryTriggerInteraction.Ignore))
    66.                     {
    67.  
    68.                         Debug.DrawRay(transform.position, direction);
    69.  
    70.                         if (!detected.Contains(hit.collider.gameObject.GetComponent<Entity>())) detected.Add(hit.collider.gameObject.GetComponent<Entity>());
    71.                         Debug.DrawRay(transform.position, transform.forward, Color.blue);
    72.                         foreach (Entity cared in detected)
    73.                         {
    74.                             if (cared.relationShipInt != selfActor.relationShipInt)
    75.                             {
    76.                                 // selfActor.BooleanManag(0);
    77.                                 if (!enemyEntities.Contains(cared)) enemyEntities.Add(cared);
    78.                             }
    79.  
    80.                         }
    81.  
    82.                     }
    83.                 }
    84.             }
    85.         }
    86.         private void OnTriggerExit(Collider other)
    87.         {
    88.             targetEntity.Remove(other.gameObject.GetComponent<Entity>());
    89.         }
    90.     }
    91. }
    Yes. I'm using 8 directional sprites for my game. It isn't a Doom clone tho. (Which I see is good)
     
  2. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    Main issue solved. I forgot to add the layer "Detectable" to the character. But is the script improve-able either way?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    All scripts are improvable but I don't see anything obviously problematic. Fortunately this is software, which means it's SOFT! As you use this, if you start to find issues that make it difficult to use, don't be afraid to change stuff. It's broadly called "refactoring."
     
  4. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    Sure it's improve-able. The enemy detects the player instantly when it enters the sphere collider. Even if it looked away.