Search Unity

Raycast Hit Help.

Discussion in 'Physics' started by TheMonkeysaur, Dec 7, 2017.

  1. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    I am trying to get the object which is hit by my raycast, but I'm having some trouble.

    All of the other examples I have seen people using seem to involve and additional "hit" in the Physics.Raycast method but I can't seem to add it, and when I create a RaycastHit = hit variable I can't seem to figure out how to define it as the object the Raycast collided with.

    This is my code with annotations showing what I am trying to do, nothing I have tried so far works, any help is greatly appreciated.

    Code (csharp):
    1.  
    2.         if (Input.GetKeyDown(KeyCode.D)) //Left
    3.         {        
    4.             if (!Physics.Raycast(transform.position, new Vector3(-2f, 0f, 0f), 2f) && (canMove)) // Space is free
    5.             {
    6.                 transform.forward = new Vector3(-1f, 0f, 0f); //Player transform change
    7.                 transform.Rotate(0, 90, 0); //Player rotates
    8.                 MoveLeft(); //Player moves left
    9.             }
    10.             else if (Physics.Raycast(transform.position, new Vector3(-2f, 0f, 0f), 2f) && (canMove)) // There is an object here
    11.             {
    12.                 //RaycastHit hit;
    13.                 //hit = ???
    14.                 //if (hit.collider.tag == "Block") //If the object is a block
    15.                 //{
    16.                     //thisBlockMove = hit.GetComponent<BlockMove>(); //Get the Block Move component for this block
    17.                     //thisBlockMove.MoveLeft(); // Move this block left
    18.                 //}
    19.  
    20.             }
    21.         }
    22.  
     
  2. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    You would use raycast to find a ray cast hit position, like mouse click. Clicking on a posion on a plane would cast a ray ad move the player to that position, like this code..

    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    2.                     RaycastHit hitInfo;
    3.                     if (Physics.Raycast (ray, out hitInfo)) {
    4.  
    5.  
    6.                         GetComponent<NavMeshAgent> ().SetDestination (hitInfo.point);
    7.  
    8.                     }
     
  3. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    Thanks, I've seen that kind of example, but my (Physics.Raycast) doesn't want an "Out" or "RaycastHit", it wants a Vector 3 and a distance, this is basically where I am confused.
     
  4. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    But by you clicking the "D" button, what are you hitting with your ray...
     
  5. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    On "D" the player is either moving left, not moving or doing anything if there is a wall there, or pushing a block left if there is a block there.

    What I'm doing is firing a raycast over a short distance, and a) deciding whether the player can move Left (if the space is free (!Physics.Raycast)), and b) if the space is not free, (if Physics.Raycast) checking what is there (a wall or a cube) and if it's a cube, I'm telling the cube to move left.

    In order to do that, I need to get the cubes movement component and run a method from it.

    Checking if the space is empty or if there is something there works fine, but I am struggling to get the info about what was hit and then using that info to access the hit object and run a method from one of it's components.

    I hope that makes sense, and thanks for your help.
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    What you are experiencing is known as "function overloading" or "method overloading" where you can write the same function but give it different parameters. Even a small glance at the manual will show you the number of different ways to use it them, but you can't make up your own way to use them.

    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html shows some and autocomplete shows 16 variations.

    upload_2017-12-8_12-36-4.png
     
  7. TheMonkeysaur

    TheMonkeysaur

    Joined:
    May 2, 2017
    Posts:
    52
    Ok! I have to be honest, this concept is new to me, so thanks for pointing it out!

    Now I understand why others seemed to be using the same function with different parameters, making more sense now, thanks for your help.
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yes it's trivial, just write the same function twice to try it yourself:

    Code (CSharp):
    1.     void PrintNumber(float a)
    2.     {
    3.         Debug.Log(a);
    4.     }
    5.  
    6.     void PrintNumber(float a, float b)
    7.     {
    8.         Debug.Log(a + b);
    9.     }
    So here we have the same function name that can be used in 2 different ways. Turns out the compiler and language design allows us to do this.
     
    TheMonkeysaur likes this.