Search Unity

Raycast Explanation

Discussion in 'Scripting' started by Preetthefire, Sep 14, 2012.

  1. Preetthefire

    Preetthefire

    Joined:
    Apr 12, 2012
    Posts:
    83
    Good morning everyone,
    I was surfing on web, and i have see raycast, i searched a lot on the unity scripting reference, but nothing explain this nicely.
    I have see that we can use for ScreenToRay, what is that?
    we can use for detecting if is in the range, how? and why dont use vector3.distance?
    we can use for enemy ai scripting? how?
    we can use for shooting projectiles? but why dont use instantiation, and apply force on rigidbody?
    what is really this raycast? what can i do? and how?
    what is the scope? thanks everyone, please tell me more then the web on raycasts!!
    thanks have a nice day!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There is some information about raycasting here in the manual. Most of the useful information is returned in the RaycastHit object - you can use this to identify the target, get its distance, etc.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    A ray is a more complex structure than a vector.

    Vector - has direction and magnitude
    Ray - has direction, location, and infinite magnitude (implicit)

    It can be used to describe a line in 3-space.

    A raycast is when you take a ray and project it into 3-space and find all intersecting geometry. When you do a Raycast you usually limit the distance of the ray, because an infinite ray would never finish computing.

    Think of it like if you were to look in a direction. You have a location, you have a direction, and you can see for a certain distance in front of you. This raycast represents that calculation... finding what objects in the world intersect your view (are seen).

    It can be used for all sorts of stuff that you described. Note a Ray is a mathematical structure, there is a datatype in code for representing them, but you don't HAVE to use said construct all the time. For instance a throw usually has a magnitude, direction, and location... but the location is calculated every time you throw. So really you only store the direction and magnitude long term... which is a vector.



    Calculating what your ray is, is up to you.

    ScreenToRay though helps in this because when considering the camera you have a projection matrix in your way that complicates stuff. ScreenToRay uses that projeciton matrix to calculate a ray for you. So the projection matrix creates what is called a viewing frustum. It looks like a pyramid with the top cut off. The small top is the 'near plane' the large base is the 'far plane'... representing the nearest you can see and the farthest you can see respectively. When you calculate ScreenToRay for some 2d vector what you're doing is getting a ray from the camera that would pass through this 2d point inverse projected on the near and far planes of this frustum.

    From said ray you can cast into the 3d world and find things underneath the mouse in 3-space.
     
  4. Preetthefire

    Preetthefire

    Joined:
    Apr 12, 2012
    Posts:
    83
    Thank you so much, this helped me a lot. Very nice explanation from "lordofduct" and good link from "andeeeee". Thanks.
    Can I ask you a question?
    I am wanting to create an enemy ai, that follows the player. The environment are all corridors with some halls, so i have created waypoint system (based on SeekSteer of UnifyCommunity). But i have to create the chase system, The enemy follow the player if in range and if is in front. I have see this video but impossible to get help from this: Youtube Broadcast Video Link.
    Could you help me please? Thanks, See you Bye!
     
    Last edited: Sep 15, 2012