Search Unity

Check if enemy can see player/walk to point

Discussion in 'Editor & General Support' started by DamnICantFly, Sep 30, 2014.

  1. DamnICantFly

    DamnICantFly

    Joined:
    Sep 27, 2013
    Posts:
    12
    Hey guys,

    I'm having some trouble with checking if an enemy can see the player (and is able to walk towards him without hitting any walls). I've already wrote a path-finding system to make the enemy move, but as soon as the player is in sight, the enemy should walk to the player instead of following the path.

    In the picture you can see the red lines which are Debug.DrawLine's and the green lines which are the lines how I want them to be. The green circles are the circle colliders attached to the player and enemy.
    If I only linecast from the middle of the enemy and the middle of the player, the enemy will move against the wall. Which is something I'd like to prevent ;)

    I have no idea anymore on how to do this so I hope someone here can help me :) LookAtPlayer2.png

    Code (CSharp):
    1. Vector3 start,end;
    2.  
    3. start = transform.position+Quaternion.FromToRotation(transform.position,player.position)*Vector3.up*0.32f;
    4. end = player.position+Quaternion.FromToRotation(transform.position,player.position)*Vector3.up*0.32f;
    5. Debug.DrawLine(start,end,Color.red);
    6.  
    7. start = transform.position+Quaternion.FromToRotation(transform.position,player.position)*-Vector3.up*0.32f;
    8. end = player.position+Quaternion.FromToRotation(transform.position,player.position)*-Vector3.up*0.32f;
    9. Debug.DrawLine(start,end,Color.red);
     
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    I think you need to look at transform.TransformPoint, it's in the docs, that way you can test from the sides of the unit regardless of facing.
     
  3. DamnICantFly

    DamnICantFly

    Joined:
    Sep 27, 2013
    Posts:
    12
    Hey Arowx!

    Thank you for your reply :) After some research I've finally found a solution to make it work!
    A problem was that transform.TransformPoint looks at the current rotation from the enemy, instead of the potential target rotation it might use. This is why I made a tempGameObject and made it look at the potential target position.
    I'm not sure if this is the best way, but at least this works for me. If you know a better way, please let me know :)

    Code (CSharp):
    1. Vector3 start, end, dif;
    2.  
    3. GameObject tempGameObject = new GameObject();
    4. Vector3 vectorToTarget = PlayerScript.POSITION - transform.position;
    5. tempGameObject.transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg);
    6. tempGameObject.transform.position = transform.position;
    7.  
    8. start = tempGameObject.transform.TransformPoint(Vector3.up*0.32f);
    9. dif = start - tempGameObject.transform.position;
    10. end = Misc.Player().transform.position + dif;
    11. Debug.DrawLine(start,end,Color.red);
    12.  
    13. start = tempGameObject.transform.TransformPoint(Vector3.up*-0.32f);
    14. dif = start - tempGameObject.transform.position;
    15. end = Misc.Player().transform.position + dif;
    16. Debug.DrawLine(start,end,Color.red);
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194