Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Using sqrmagnitude correctly

Discussion in 'Scripting' started by PhantomProgramming, Mar 27, 2019.

  1. PhantomProgramming

    PhantomProgramming

    Joined:
    Jan 16, 2019
    Posts:
    60
    So I have this script here and I was wondering how to substitute sqrmagnitude with overlapsphere.
    Any ideas?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6.  
    7.  
    8. public class AILocate : MonoBehaviour
    9. {
    10.  
    11.  
    12.     public LayerMask detectionLayer;
    13.     private Transform myTransfom;
    14.     public NavMeshAgent myagent;
    15.     private Collider[] hitColliders;
    16.     private float checkRate;
    17.     private float nextCheck;
    18.     public float detectionRadius = 180;
    19.     public bool moving = false;
    20.     public float extraRotationSpeed;
    21.     public float AI_Delay = 0.1f;
    22.  
    23.  
    24.  
    25.  
    26.  
    27.     // Start is called before the first frame update
    28.     void Start()
    29.     {
    30.         AI_Delay = Random.Range(0.1f, 0.8f);
    31.         myagent.enabled = true;
    32.         sir();
    33.         StartCoroutine("DelayEnemy");
    34.     }
    35.  
    36.  
    37.  
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.  
    43.  
    44.  
    45.     }
    46.     void FixedUpdate()
    47.     {
    48.  
    49.        
    50.  
    51.         //CheckIfInRange();
    52.        // myagent.enabled = true;
    53.  
    54.  
    55.     }
    56.  
    57.  
    58.     void sir()
    59.     {
    60.         myTransfom = transform;
    61.         myagent = GetComponent<NavMeshAgent>();
    62.         checkRate = Random.Range(0.2f, 0.5f);
    63.     }
    64.  
    65.     public IEnumerator DelayEnemy()
    66.     {
    67.  
    68.         while(true){
    69.  
    70.             yield return new WaitForSeconds(1);
    71.             if (Time.time > nextCheck && myagent.enabled && myagent.pathPending == false)
    72.             {
    73.                 nextCheck = Time.time + checkRate;
    74.  
    75.                 //Code, I want to change:
    76.                 hitColliders = Physics.OverlapSphere(myTransfom.position, detectionRadius, detectionLayer);
    77.  
    78.  
    79.                 if (hitColliders.Length > 0 && myagent.pathPending == false)
    80.                 {
    81.                     myagent.destination = hitColliders[Random.Range(0, hitColliders.Length - 1)].transform.position;
    82.                     //myagent.SetDestination(hitColliders[Random.Range(0, hitColliders.Length - 1)].transform.position);
    83.                     moving = true;
    84.  
    85.                 }
    86.                 else { moving = false; }
    87.  
    88.  
    89.  
    90.  
    91.              
    92.  
    93.             }
    94.         }
    95.      
    96.  
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    869
    questions on sqrMagnitude usually are related to Vector3. sqrMagnitude in that context can be used to check distance comparisons:
    Code (CSharp):
    1. //transforms which we check distance for
    2. public Transform obj1;
    3. public Transform obj2;
    4.  
    5. //limits
    6. float limit = 2;
    7. float sqrLimit = limit * limit;//this equals 4
    8.  
    9. //somewhere in the script
    10. Vector3 delta = obj1.position - obj2.position;
    11. if(delta.sqrMagnitude <= sqrLimit){
    12.     //do something
    13. }
    In the above example I'm checking if the distance between obj1 and obj2 is less than or equal to 2.

    You are using SphereOverlap to detect what is in range in the first place, and it does not seem you have a manager which contains all objects in the scene. Using sqrMagnitude for distance checking requires knowing what objects you are comparing before hand.

    So far this sounds like a misguided quest of micro-optimisation. If thats not the case however, you should reveal more of what you are trying to solve or achieve
     
unityunity