Search Unity

Ray cast from camera to player

Discussion in 'Scripting' started by ChuckieGreen, Apr 11, 2021.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    Hi, I am wanting to set up a raycast from camera to player, but im having an issue. I want to draw the ray, but it is not drawing until the ray intersects with another object, then it draws the ray from camera to player. The player has a collider, so I dont understand why Physics.Raycast is returning false until the ray hits another object (I am assuming that is what is happening). If I have the DrawLine code outside of the if statement it does draw, so it is something to do with the if statement, but cant figure out what. Does anyone possible know what I am doing wrong? Thanks in advance o_O



    Code (CSharp):
    1.  
    2.   private GameObject cam1;
    3.  
    4.  
    5. void update {
    6. if (Physics.Raycast(cam1.transform.position, player.transform.position , Mathf.Infinity))
    7.         {
    8.          //   Debug.DrawRay(transform.position, player.transform.position, Color.yellow);
    9.             Debug.DrawLine(cam1.transform.position, player.transform.position, Color.yellow);
    10.             Debug.Log("test");
    11.         }
    12. }
    13.  
     
  2. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    Hi, thanks for the reply. Shouldnt the if statement return true every frame and then the drawline occur every frame?

    Also it is the Update method, i had just missed out copying it when i copy/pasted here, so just typed it here, and mistyped. In the code it is correct.
     
  3. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Yeah, I deleted my comment just as your reply went up, as I saw I had misconstrued your code. Sorry about that.
     
  4. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    No problem :)
     
  5. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Why you don't use a RaycastHit so you can have informations like What gameObject you are hitting, etc...

    https://docs.unity3d.com/ScriptReference/RaycastHit.html
     
  6. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Ah, I think maybe I see the problem now: Your second argument to RayCast is the player's position, but you need that to be a direction, not a location. Try this:

    if (Physics.Raycast(cam1.transform.position, player.transform.position - cam1.transform.position, Mathf.Infinity))
     
    alexeu likes this.
  7. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    True !
     
  8. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
    Thank you so much, its finally working.
     
  9. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    358
  10. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Glad to help!

    It can be confusing to know when you are dealing with absolute coordinates as opposed to simply directions, particularly when both are stored in a struct called a "Vector3." Any ordered list of values is a vector, but I would guess that most of us think of vectors not as numbers, but things like this:

    upload_2021-4-11_15-31-31.png

    That's as opposed to a point with its position defined by coordinates, which I think of as things like this:

    upload_2021-4-11_15-37-12.png

    But in Unity, both are described the same way: with a Vector3. So you have to read the doc closely to know which usage they want.
     
    ChuckieGreen likes this.