Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to check tag with raycast2d

Discussion in 'Scripting' started by svyathor, Jan 18, 2020.

  1. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    32
    Hollow I am making a 2d game. I need enemy to shoot. I made it with raycast 2d and now I need to check tag of GameObject that was hit. Please help me
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
  3. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    32
    Code (CSharp):
    1. public const float Seconds = 0.02f;
    2.     public LayerMask hittableLayer;
    3.     public Transform firePoint;
    4.     public int damage = 1;
    5.     public GameObject impactEffect;
    6.     public LineRenderer lineRenderer;
    7.     public int shootDist;
    8.  
    9.  
    10.  
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void FixedUpdate()
    18.     {
    19.  
    20.         RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right, shootDist, hittableLayer);
    21.  
    22.         if (hitInfo != null)
    23.            
    24.         {
    25.             if (hitInfo == GameObject.FindGameObjectWithTag("Player"))
    26.             {
    27.                 lineRenderer.enabled = true;
    28.                 StartCoroutine(Shoot1());
    29.                 Debug.Log("Player");
    30.             }
    31.             else if (hitInfo == GameObject.FindGameObjectWithTag("obstacle")) ;
    32.             {
    33.                 //lineRenderer.enabled = false;
    34.                 Debug.Log("obstacle");
    35.             }
    36.  
    37.         }
    38.        
    39.  
    40.        
    41.     }
    42.  
    43.     IEnumerator Shoot1()
    44.     {
    45.  
    46.         RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right, shootDist, hittableLayer);
    47.  
    48.  
    49.         if (hitInfo != null)
    50.         {
    51.             HealthBar health = hitInfo.transform.GetComponent<HealthBar>();
    52.  
    53.             if (health != null)
    54.             {
    55.                 health.TakeDamage(damage);
    56.             }
    57.             Instantiate(impactEffect, hitInfo.point, Quaternion.identity);
    58.  
    59.             lineRenderer.SetPosition(0, firePoint.position);
    60.             lineRenderer.SetPosition(1, hitInfo.point);
    61.  
    62.             Debug.Log("shoot");
    63.         }
    64.  
    65.  
    66.         lineRenderer.enabled = true;
    67.  
    68.  
    69.         yield return new WaitForSeconds(Seconds);
    70.  
    71.         lineRenderer.enabled = false;
    72.  
    73.  
    74.  
    75.     }
    76. }
    77.  
    thanks for your answer. I made a code for enemy. And when the obstacle is in front of enemy ,console write shoot, player and obstacle. And line render is true but it shoot to the obstacle
     
  4. svyathor

    svyathor

    Joined:
    Aug 18, 2019
    Posts:
    32
    I solved this problem.
    Code (CSharp):
    1. RaycastHit2D hitInfo = Physics2D.Raycast(firePoint.position, firePoint.right, shootDist, hittableLayer);
    2.  
    3.         if (hitInfo != null)
    4.            
    5.         {
    6.             if (hitInfo.collider.tag == "Player")
    7.             {
    8.                 lineRenderer.enabled = true;
    9.                 StartCoroutine(Shoot1());
    10.             }
    11.  
    12.  
    13.         }
     
  5. bardicbytes

    bardicbytes

    Joined:
    Dec 27, 2012
    Posts:
    1
    FYI, This won't work.
    hitInfo != null alwyas returns true because hitInfo is type RaycastHit2D. RaycastHit2D is a struct. structs can't be null.
    Maybe check hitInfo.collider, beause that may be null.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Note that this is an old post but you can and should simply do:
    Code (CSharp):
    1. if (hitInfo)
    2. {
    3.     ... magic ...
    4. }
    ... as it has an implicit bool operator overload that checks, as you say, "collider != null".
     
    Kurt-Dekker likes this.