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

Question check if raycast hitted object is a certain tag and then do something

Discussion in 'Scripting' started by JerMurder, Oct 31, 2021.

  1. JerMurder

    JerMurder

    Joined:
    Jan 5, 2021
    Posts:
    70
    Hi
    i have this script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragRigidbody : MonoBehaviour
    6. {
    7.     public float forceAmount = 500;
    8.  
    9.     Rigidbody selectedRigidbody;
    10.     Camera targetCamera;
    11.     Vector3 originalScreenTargetPosition;
    12.     Vector3 originalRigidbodyPos;
    13.     float selectionDistance;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         targetCamera = GetComponent<Camera>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (!targetCamera)
    24.             return;
    25.  
    26.         if (Input.GetMouseButtonDown(0))
    27.         {
    28.             //Check if we are hovering over Rigidbody, if so, select it
    29.             selectedRigidbody = GetRigidbodyFromMouseClick();
    30.         }
    31.         if (Input.GetMouseButtonUp(0) && selectedRigidbody)
    32.         {
    33.             //Release selected Rigidbody if there any
    34.             selectedRigidbody = null;
    35.         }
    36.     }
    37.  
    38.     void FixedUpdate()
    39.     {
    40.         if (selectedRigidbody)
    41.         {
    42.             Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition;
    43.             selectedRigidbody.velocity = (originalRigidbodyPos + mousePositionOffset - selectedRigidbody.transform.position) * forceAmount * Time.deltaTime;
    44.         }
    45.     }
    46.  
    47.     Rigidbody GetRigidbodyFromMouseClick()
    48.     {
    49.         RaycastHit hitInfo = new RaycastHit();
    50.         Ray ray = targetCamera.ScreenPointToRay(Input.mousePosition);
    51.         bool hit = Physics.Raycast(ray, out hitInfo, 10.0f);
    52.         if (hit)
    53.         {
    54.             if (hitInfo.collider.gameObject.GetComponent<Rigidbody>())
    55.             {
    56.                 selectionDistance = Vector3.Distance(ray.origin, hitInfo.point);
    57.                 originalScreenTargetPosition = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance));
    58.                 originalRigidbodyPos = hitInfo.collider.transform.position;
    59.                 return hitInfo.collider.gameObject.GetComponent<Rigidbody>();
    60.             }
    61.         }
    62.  
    63.         return null;
    64.     }
    65. }
    66.  
    and on row 52 it says if (hit)
    could i somehow change it so if it hits an gameobject with a certain tag it would do something
     
  2. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    Code (csharp):
    1. if (hit && hitInfo.collider.gameObject.tag == "my_tag")
    should do the trick. You can check any number of other conditions as well.

    This is very similar to the check on line 54 where you make sure that the object has a RigidBody.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    You mean like this?
    Code (CSharp):
    1. bool hit = Physics.Raycast(ray, out hitInfo, 10.0f);
    2. if (hit && hitInfo.collider.CompareTag("MyTag"))
    3. {
    4.     if (hitInfo.collider.gameObject.GetComponent<Rigidbody>())
    5.     {
    6.     // Do stuff.
    7.     }
    8. }
    EDIT: Two identical suggestions at the same time. :)
     
    ubbelito likes this.
  4. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    Yes but "CompareTag" seems like a more Unity way of doing things. Thanks!
     
    MelvMay likes this.
  5. JerMurder

    JerMurder

    Joined:
    Jan 5, 2021
    Posts:
    70
    Thank u both so much <3