Search Unity

Checking between two raycasts

Discussion in 'Scripting' started by SixPence, Feb 4, 2013.

  1. SixPence

    SixPence

    Joined:
    Nov 6, 2012
    Posts:
    2
    hi Folks,

    So im trying to create a patrolling enemy using two raycasts, trying to create a field of vision.

    Vector3 DirectionRayRight = transform.TransformDirection(Vector3.forward - Vector3.right);
    Debug.DrawRay (transform.position, DirectionRayRight * Range, Color.green);
    Vector3 DirectionRayLeft = transform.TransformDirection(Vector3.forward - Vector3.left);
    Debug.DrawRay (transform.position, DirectionRayLeft * Range, Color.red);

    this code sends one raycast on 45 angle and the other on a -45 angle, the player is only noticed if they are in the field of vision.
    my question is how do I check between the two angles.
    am i going about this the wrong way.
    cheers,
    SixPence.
     
  2. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    raycast will only work if it collides with the the player.

    First do some math and compare the point of the player and the point of the bad guy. if they are within the the 90 degree field of view fire a raycast between bad guy and player. If it hits the player first no object is blocking the view, if it hits something else first he is at least partly obscured.
     
  3. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    I will give you a hint! Vector dot product
     
  4. SixPence

    SixPence

    Joined:
    Nov 6, 2012
    Posts:
    2
    thanks for the hints folks, but i came to the conclusion that i can just use a cone or similar shaped object, so when the player comes in contact with the cone, BAM!

    thanks again,