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

RaycastHit Transform Tag returning GameObject name?

Discussion in 'Scripting' started by thesupersoup, Jul 15, 2018.

  1. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    70
    Hello,

    Consider the following code:
    Code (CSharp):
    1. [Command]
    2. public void CmdFireCheck(Vector3 viewPos, Vector3 viewFwd, bool wType, int wDmg, int wRng, float pSpr, int nRepeat) // CmdFireCheck uses local camera position and direction, as well as a weapon's type, dmg, and range; player's current spread value, nRepeat is the number of times to repeat
    3. {
    4.     Vector3 rayDir = viewFwd;
    5.     Vector3 hitNormal;
    6.     RaycastHit hit;
    7.     GameObject target;
    8.     bool spreadShot = false;    // Shots should spread around the adjusted rayDir
    9.  
    10.     if(nRepeat > 0)
    11.         spreadShot = true;
    12.  
    13.     if(pSpr > 0.0f)
    14.     {
    15.         Vector3 dirMod = new Vector3(Random.Range(-(pSpr),pSpr),Random.Range(-(pSpr),pSpr),Random.Range(-(pSpr),pSpr));
    16.         rayDir = rayDir + dirMod;
    17.         rayDir = rayDir.normalized;
    18.     }
    19.     else
    20.         rayDir = rayDir.normalized;
    21.  
    22.     do
    23.     {
    24.         Vector3 fireRay = rayDir;
    25.      
    26.         if(spreadShot)
    27.         {
    28.             fireRay = fireRay + new Vector3(Random.Range(-(Apex.SPREAD_SHOT_BASE), Apex.SPREAD_SHOT_BASE),Random.Range(-(Apex.SPREAD_SHOT_BASE), Apex.SPREAD_SHOT_BASE),Random.Range(-(Apex.SPREAD_SHOT_BASE), Apex.SPREAD_SHOT_BASE));
    29.             fireRay = fireRay.normalized;
    30.         }
    31.      
    32.         if(Physics.Raycast(viewPos, fireRay, out hit, wRng, mask))
    33.         {          
    34.             target = hit.transform.root.gameObject;         // What we hit, fetching root because hitboxes
    35.             hitNormal = hit.normal;                            // Formerly we used Quaternion.Euler(-90,90,0), now we're using Quaternion.LookRotation(hitNormal)
    36.              
    37.             if(target.tag != "Terrain")
    38.             {
    39.                 if(target.tag == "NPC")
    40.                 {
    41.                     uint targetID = target.GetComponent<NetworkIdentity>().netId.Value;
    42.                     // Debug.Log(this.gameObject.name + " EntityPlayerNet CmdFireCheck(): Sending hit notification to " + targetID);
    43.                  
    44.                     if(!wType)
    45.                     {
    46.                         gameMasterScript.RpcSendSta(target, wDmg, this.gameObject);
    47.                         effectTemp = Instantiate(effectMeleeSFX, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    48.                         effectTemp.transform.parent = hit.transform;
    49.                         NetworkServer.Spawn(effectTemp.gameObject);
    50.                     }
    51.                     else
    52.                     {
    53.                         gameMasterScript.RpcSendDmg(target, wDmg, false, this.gameObject);    // Passing false because the damage should not be direct (bypass armor)
    54.                         effectTemp = Instantiate(effectBlood, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    55.                         effectTemp.transform.parent = hit.transform;
    56.                         NetworkServer.Spawn(effectTemp.gameObject);
    57.                     }
    58.                 }
    59.                 else if(target.tag == Apex.TAG_MAT_CONC)
    60.                 {
    61.                     Debug.Log("Tags: " + target.tag + "," + Apex.TAG_MAT_CONC + ", hit.tag is " + hit.transform.tag);
    62.                     effectTemp = Instantiate(effectConc, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    63.                     effectTemp.transform.parent = hit.transform;
    64.                     NetworkServer.Spawn(effectTemp.gameObject);
    65.                 }
    66.                 else if(target.tag == Apex.TAG_MAT_METAL)
    67.                 {
    68.                     Debug.Log("Tags: " + target.tag + "," + Apex.TAG_MAT_METAL + ", hit.tag is " + hit.transform.tag);
    69.                     effectTemp = Instantiate(effectMetal, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    70.                     effectTemp.transform.parent = hit.transform;
    71.                     NetworkServer.Spawn(effectTemp.gameObject);
    72.                 }
    73.                 else if(target.tag == Apex.TAG_MAT_WOOD)
    74.                 {
    75.                     Debug.Log("Tags: " + target.tag + "," + Apex.TAG_MAT_WOOD + ", hit.tag is " + hit.transform.tag);
    76.                     effectTemp = Instantiate(effectWood, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    77.                     effectTemp.transform.parent = hit.transform;
    78.                     NetworkServer.Spawn(effectTemp.gameObject);
    79.                 }
    80.                 else if(target.tag == Apex.TAG_MAT_SAND)
    81.                 {
    82.                     Debug.Log("Tags: " + target.tag + "," + Apex.TAG_MAT_SAND + ", hit.tag is " + hit.transform.tag);
    83.                     effectTemp = Instantiate(effectSand, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    84.                     effectTemp.transform.parent = hit.transform;
    85.                     NetworkServer.Spawn(effectTemp.gameObject);
    86.                 }
    87.                 else
    88.                 {
    89.                     Debug.Log("Tags: " + target.tag + ",???, hit.tag is " + hit.transform.gameObject);
    90.                     effectTemp = Instantiate(effectGeneric, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    91.                     effectTemp.transform.parent = hit.transform;
    92.                     NetworkServer.Spawn(effectTemp.gameObject);
    93.                 }
    94.             }
    95.             else
    96.             {
    97.                 effectTemp = Instantiate(effectDirt, hit.point, Quaternion.LookRotation(hitNormal)) as GameObject;
    98.                 // Debug.Log(this.gameObject.name + " EntityPlayerNet CmdFireCheck(): effectTemp is " + effectTemp);
    99.                 NetworkServer.Spawn(effectTemp.gameObject);
    100.                 // Debug.Log(this.gameObject.name + " EntityPlayerNet CmdFireCheck(): " + hit.collider + " was hit!");
    101.             }
    102.         }
    103.         // else
    104.             // Debug.Log(this.gameObject.name + " EntityPlayerNet CmdFireCheck(): Nothing was hit with the raytrace");
    105.      
    106.         nRepeat--;
    107.      
    108.     }while(nRepeat > 0);
    109. }
    110.  
    With the following constants:
    Code (CSharp):
    1.     public const string /* Tag specific string consts */
    2.                         TAG_MAT_CONC = "MatConc",
    3.                         TAG_MAT_DIRT = "MatDirt",
    4.                         TAG_MAT_FLESH = "MatFlesh",
    5.                         TAG_MAT_METAL = "MatMetal",
    6.                         TAG_MAT_ROCK = "MatRock",
    7.                         TAG_MAT_SAND = "MatSand",
    8.                         TAG_MAT_WATER = "MatWater",
    9.                         TAG_MAT_WOOD = "MatWood";
    When shooting the pictured GameObject,
    wall.PNG
    I get the following output:
    Code (CSharp):
    1. Tags: Untagged,???, hit.tag is ConcreteWallBackRight (UnityEngine.GameObject)
    What simple solution am I missing?
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    I guess it's caused because you use transform.root. Depending on your scene hierarchy and where the target is located in that hierarchy, it might not return what you would expect.

    It does not necessarily return the GameObject/Prefab root, but the topmost transform in the hierarchy.
    https://docs.unity3d.com/ScriptReference/Transform-root.html

    Imagine the following hierarchy, where each of these items represents a GameObject:
    Code (CSharp):
    1. Town
    2. - Props
    3. -- Hydrant
    4. --- Mesh
    Calling transform.root on the "Mesh", does not return "Hydrant" which could be its prefab root, but "Town" because it's the topmost transform in the hierarchy.
     
    thesupersoup likes this.
  3. thesupersoup

    thesupersoup

    Joined:
    Nov 27, 2017
    Posts:
    70
    That was 100% the issue, thank you.

    For others who thought like myself that it would return the root of the prefab, learn from my rookie mistakes!
     
    Peter77 likes this.