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

Raycast knowing what object its hitting?

Discussion in 'Scripting' started by Pantomorphic, Oct 19, 2014.

  1. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    How do I make it so that the raycast knows what game object its hitting? I am making a Portal clone and I am trying to make it so that the portal wont shoot onto other portals and certain types of walls. Here is the code:
    Code (CSharp):
    1. int x = Screen.width / 2;
    2. int y = Screen.height / 2;
    3.  
    4. Ray ray = camera.ScreenPointToRay(new Vector3(x,y));
    5. RaycastHit hit;
    6. if(Physics.Raycast(ray, out hit))
    7. portal.transform.position = hit.point;
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    hit.collider.gameObject
     
  3. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    Thanks, but how would this look in an if statement? (sorry kinda new)
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Code (CSharp):
    1. int x = Screen.width / 2;
    2. int y = Screen.height / 2;
    3. Ray ray = camera.ScreenPointToRay(new Vector3(x,y));
    4. RaycastHit hit;
    5. if(Physics.Raycast(ray, out hit))
    6. portal.transform.position = hit.point;
    7. GameObject hitObject = hit.collider.gameObject;
    8. string hitObjectName = hitObject.name;
    9. if(hitObjectName == "Wall1"){
    10.  
    11. }
    12.  
     
    Last edited: Oct 19, 2014
  5. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    OK, so I implemented what you said, and here is the new code.
    Code (CSharp):
    1. int x = Screen.width / 2;
    2. int y = Screen.height / 2;
    3.      
    4. Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(newVector3(x,y));
    5. RaycastHit hit;
    6. GameObject hitObject = hit.collider.gameObject;
    7. string hitObjectName = hitObject.name;
    8. if(Physics.Raycast(ray, out hit))
    9. {
    10.     if(hitObjectName == "Wall")
    11.         {
    12.         Quaternion hitObjectRotation = Quaternion.LookRotation(hit.normal);
    13.         portal.transform.position = hit.point;  
    14.         portal.transform.rotation = hitObjectRotation;
    15.         }
    16.     if(hitObjectName == "Floor")
    17.         {
    18.         Quaternion hitObjectRotation = Quaternion.LookRotation(hit.normal);
    19.         portal.transform.position = hit.point;  
    20.         portal.transform.rotation = hitObjectRotation;
    21.         }
    22.     if(hitObjectName == "Ceiling")
    23.         {
    24.         Quaternion hitObjectRotation = Quaternion.LookRotation(hit.normal);
    25.         portal.transform.position = hit.point;  
    26.         portal.transform.rotation = hitObjectRotation;
    27.         }
    So basically, before it moves the portal object, it will check to see if it will be moving on the right object. When I run the game, it says: Null Reference Exception: Object reference not set to an instance of an object. It could be that I am making this game on a Mac because it also said there was inconsistency in the code and that some were written for Mac and some were for Windows.
     
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    It's because you need to put my code in the if statement.

    See how Physics.Raycast has "out hit" ? That means on raycast, output the information to hit object. So before that happens it will always be null unless otherwise set
     
  7. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    I am not sure I understand. Basically my angle is for the ray cast to check if its the right object to move the portal to before it moves. Objects labeled "Wall", "Floor", and "Ceiling" will be the only objects you can put portals on. Is your code putting the portal on something, and then checking if its the wrong object?
     
    Last edited: Oct 20, 2014
  8. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Code (CSharp):
    1.     GameObject hitObject = hit.collider.gameObject;
    2.     string hitObjectName = hitObject.name;
    3.  
    4.  
    5.  
    must be after

    Code (CSharp):
    1. if(Physics.Raycast(ray, out hit))
    2. {
     
  9. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    So...
    Code (CSharp):
    1. int x = Screen.width / 2;
    2. int y = Screen.height / 2;
    3.    
    4. Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(newVector3(x,y));
    5. RaycastHit hit;
    6. if(Physics.Raycast(ray, out hit))
    7. {
    8. GameObject hitObject = hit.collider.gameObject;
    9. string hitObjectName = hitObject.name;
    10. {
    11.     if(hitObjectName == "Wall")
    12.         {
    13.         Quaternion hitObjectRotation = Quaternion.LookRotation(hit.normal);
    14.         portal.transform.position = hit.point;
    15.         portal.transform.rotation = hitObjectRotation;
    16.         }
    17.     if(hitObjectName == "Floor")
    18.         {
    19.         Quaternion hitObjectRotation = Quaternion.LookRotation(hit.normal);
    20.         portal.transform.position = hit.point;
    21.         portal.transform.rotation = hitObjectRotation;
    22.         }
    23.     if(hitObjectName == "Ceiling")
    24.         {
    25.         Quaternion hitObjectRotation = Quaternion.LookRotation(hit.normal);
    26.         portal.transform.position = hit.point;
    27.         portal.transform.rotation = hitObjectRotation;
    28.         }}}
    should work?
     
  10. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
  11. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    So I copied and pasted the exact code, and nothing changed. I can still shoot portals onto every surface, including the portals themselves. Also, it says to put a semicolon after every if statement that checks the object name. When I put them in, it still says to put a semicolon in the same places.
     
  12. Kalaskrysset

    Kalaskrysset

    Joined:
    May 16, 2014
    Posts:
    9
    Instead of having a bunch of if statements checking for tags you could use a LayerMask.
    Code (CSharp):
    1.  
    2. public LayerMask canShootAt;
    3.  
    Code (CSharp):
    1.  
    2. int x = Screen.width / 2;
    3. int y = Screen.height / 2;
    4. Ray ray = camera.ScreenPointToRay(new Vector3(x,y));
    5. RaycastHit hit;
    6. if(Physics.Raycast(ray, out hit, canShootAt))
    7. portal.transform.position = hit.point;
    Now you put the things you dont want to be able to shoot at on a new layer. Then exclude that layer from the layermask in the inspector.
     
    Deleted User likes this.
  13. Deleted User

    Deleted User

    Guest

    You can also use game tag instead of layer mask if you want too.
     
  14. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    So for objects I want to be able to shoot portals on I put the code:
    Code (CSharp):
    1. public LayerMask canShootAt
    And I just leave objects that I don't want to be able to shoot on?
     
  15. Kalaskrysset

    Kalaskrysset

    Joined:
    May 16, 2014
    Posts:
    9
    No, that line is in your shooting script. Then when you look at that script in the inspector there will be a drop down-menu where you can select what layers the raycast will hit.

    Have a look at this video: (skip ahead to 10min for the layermask part)
     
  16. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    I feel very silly now, because apparently there has been a layer labeled "Ignore Raycast" this whole time. No script required.
     
  17. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    If you're using layers that will sometimes result in no ray hit, make sure to check if the ray is null or not before continuing!

    if(ray == null){
    //hit nothing
    }else{
    //Add portal
    }
     
  18. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    Oh so the raycast will go through the wall without this code?
     
  19. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Actually. Nevermind my last post. I don't know what I was thinking lol :p
     
  20. Pantomorphic

    Pantomorphic

    Joined:
    May 8, 2013
    Posts:
    53
    I don't think you are that far off, because the Raycast will shoot through you are not supposed to shoot at and put a portal on a shootable wall behind it. I'll have to design the levels with this in mind.