Search Unity

Identifying GameObject.Tag through raycasting

Discussion in 'Scripting' started by yahadi, Aug 22, 2009.

  1. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    I want to use raycast to get the tag of the game object that is in front of the player and if the player is closer than .5 meter to it and the player presses the action key then do a certain action.

    I looked through the scripting reference on raycast and raycasthit but I couldn't figure out how to retrive the tag of the hit object.
     
  2. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    It depends on which language you use and how you are performing this raycast.

    In a general case, people create a ray that goes from the camera, through it to the mouse cursor. Then, they use Phyics.Raycast and feed this ray into it. That function has an 'out' param called hitInfo (you can call it what you want really as long as you declare it beforehand so you can access it after).

    Here is an example C#

    Code (csharp):
    1.  
    2. private RaycastHit hit;
    3.  
    4. Physics.Raycast( Camera.main.ScreenPointToRay( Input.mousePosition ), out hit, 1000f );
    5.  
    6. // Then you could find your GO with a specific tag by doing something like:
    7. if(hit.gameObject.tag == "Player") {
    8.     // Do stuff.
    9. }
    10.  
     
  3. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    Thanks for the reply Digitalos,

    I should have been more clear in my post. The game I am making is a third person orbit cam where the player can orbit the camera around the character, so the ray should come out of the player himself and go directly in front and not screen relative.

    Also, I am using javascript.
     
  4. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    ok, I used this code for javascript and my third person character that the user can orbit the camera around:

    Code (csharp):
    1.         var hit : RaycastHit;
    2.    
    3.     if (Physics.Raycast (transform.position, Vector3.forward, hit))
    4.     {
    5.     print (hit.collider.tag);
    6.     Debug.DrawLine (transform.position, hit.collider.transform.position,Color.red);
    7.     }
    8.  
    Now the problem is that the line is drawn always in front of the player, if the character rotates to the opposite direction the line remains where it was orginaly shot.

    Also, the hit location is now the middle of the target object (so if you are in front of a thick wall the ray hits the middle of the brick and not the very first surface the player sees in front of him) [/code]
     
  5. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    any suggestions for this?
     
  6. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    I had a problem like this before, look at the Third PLatform tutorial, the main character has a attack script that you could get something from.
     
  7. Digitalos

    Digitalos

    Joined:
    Jun 1, 2009
    Posts:
    112
    Hiya, it's probably because you are raycasting with the world forward (z-axis) position. Convert it from world space to local and then cast with it. Check the script reference for "Transform". It has the functions you need and also yes, check the 2D Platformer tutorial on the Unity site.
     
  8. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    I tried the following:

    Code (csharp):
    1.  
    2. relative = transform.InverseTransformDirection(transform.position);
    3.  
    4. if (Physics.Raycast (relative,Vector3.forward, hit))
    5. {
    6. print (hit.collider.tag);
    7. Debug.DrawLine (relative, hit.collider.transform.position,Color.red);
    8. }
    9.  
    so I replaced transform.position which I used earlier with a conversion of it. It draws a forward line for a second (same direction as before) then the line disappears.

    I looked at the punch script in the platformer tutorial, you basically create a gizmo and check against its collisions, but I would really like to do this with Raycasting because I know I will be using raycasting for other similar features in the future.
     
  9. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    any help on converting from world space to local space?
     
  10. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Just use transform.forward instead of Vector3.forward. It'll use the forward vector for the model's current rotation.

    Physics.Raycast(transform.position, transform.forward, hit);

    If the model isn't the one holding the script, just give the camera script the variable var player : Transform;, drag the in-game character onto it, and then use player.forward.

    If the player is created after the camera, then tag the player "Player" and set the variable like this: player = GameObject.FindWithTag("Player").transform;
     
  11. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    using transform.forward instead of Vector3.forward worked fine. Thank you GargerathSunman.
     
  12. Meldow

    Meldow

    Joined:
    Jul 23, 2012
    Posts:
    1
    3 years after this answear was created, and still helping ppl
    <3
     
    ucaduque likes this.
  13. Kenthria11

    Kenthria11

    Joined:
    May 6, 2017
    Posts:
    20
    C# shows issues btw:
    Type `UnityEngine.RaycastHit' does not contain a definition for `gameObject' and no extension method `gameObject' of type `UnityEngine.RaycastHit' could be found. Are you missing an assembly reference?
     
    Stebssbets likes this.