Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using Raycast

Discussion in 'Scripting' started by Jsim, Jun 21, 2014.

  1. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    So I am trying to get this enemy AI to chase the player, but only when there is direct line of sight between the enemy and the player (preferably I would like to make the AI chase the player when the player is actually facing the AI or at least facing a near range of the AI, but I do not know how to do this... So if you can help with this too that would be super helpful). Let me show you what I have so far, it is not very impressive, mostly because it does not work...

    Code (JavaScript):
    1. private function chaseWhenSeenBehavior(vec : Vector3){
    2.     var hit : RaycastHit;
    3.     if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit)){
    4.         if((hit.transform.position - transform.position).magnitude >= (targetPosition - transform.position).magnitude)
    5.         transform.Translate(vec*walkspeed*Time.deltaTime, Space.World);
    6.     }
    7. }
    Other information:
    "vec" is just the normalized vector of the AI facing the target.
    "targetPosition" is obviously just the transform.position of the target.

    I think this is all the information you need to understand the code segment. So yea, basically I just need my 2D AI to chase the player if there is a direct line of sight, and another function that would make the AI chase the player if the player faces the AI's general direction.

    Thanks so much, I'm still learning and this forum is so helpful to me!
     
  2. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    Bump
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I would do two things:
    1) for the distance just use Physics.OverlapSphere, you set a radius to this Sphere, if the enemies OverlapSphere catches the player's collider, that means he's close enough
    2)if he's in the sphere, to check the line of sight of your enemy towards the player: cast a ray from the enemy to the player, if the hit returns the tag player, he's insight. if it doesn't return player, it means there is something in between.
     
  4. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    How do you get the player tag from a RaycastHit? Also, doesn't suggestion 1 just make the enemy chase the AI if the Player is in a certain range? The second suggestion is more what my script should be doing. Also, I still need to find out a way to make the AI chase the player (no matter what distance the player is from the AI) if the player's rotation is looking near the AI.
     
  5. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    bump
     
  6. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    Can somebody help?
     
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    chill with the bumps...
    RaycastHit returns the collider that it's hitting... so if(hit.collider.tag == "Player")
    unless your enemies eyesight is infinite you need suggestion 1 to check if he's within visible radius.
     
  8. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    Sorry about the bumps I didn't notice that the post was still near the top. Anyways, thanks that is helpful, but how would I do "I would like to make the AI chase the player when the player is actually facing the AI or at least facing a near range of the AI" without editting the Player and its scripts?
     
  9. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Maybe :
    Code (JavaScript):
    1. if(transform.rotation == Quaternion.LookRotation(enemy.position - transform.position, Vector3.up)){
    2. Debug.Log("Facing an enemy");  
    3. }
     
  10. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    or you could do a Physics.CapsuleCast in front of your enemies instead of the OverlapSphere
     
  11. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    I need the enemy to check if the player's rotation is facing the enemy, not have the player check if he is facing an enemy
     
  12. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    the same as intense_gamer said just change enemy.position to player.position
     
  13. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    No that didn't work.
     
  14. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If you want to check if the player is facing the enemy.
    Code (csharp):
    1. Vector3 directionToEnemy = enemy.transform.position - player.transform.position;
    2. bool playerIsFacingEnemy = Vector3.Dot(directionToEnemy.normalized, player.transform.forward) > 0;
    If this dot product is bigger than zero, then it means the enemy is on the front half of the player. If this dot product is 1, then it means the player is looking directly at the enemy. If the dot product is -1, then the player is looking directly away from the enemy.
     
  15. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    I understand what you mean by this, but what I have been trying to get across is what I mean by "facing the enemy". What this seems to do is find out if the enemy is facing the player. If I walk where the enemy is facing (which it is programmed to always face the player anyways) it follows me, no matter where I am looking. I need to check if the player is looking at the enemy, take note that the player is always facing wherever the mouse is facing.

    This is what I used

    Code (JavaScript):
    1. if(Vector3.Dot((transform.position - targetPosition).normalized, Vector3.up) > 0){
    2.         transform.Translate(vec*walkspeed*Time.deltaTime, Space.World);
    3.     }
     
  16. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    sorry it's a bit hard to read english you're using... "I understand what you mean by this, but what I have been trying to get across is what I mean by "facing the enemy". "...

    anyway so one last time: you would like to let the enemy check if the player is looking at him?
    using Garth Smith's code snippet... you can see the "playerIsFacingEnemy" bool go on and off in the inspector.
     

    Attached Files:

  17. Jsim

    Jsim

    Joined:
    May 12, 2014
    Posts:
    33
    Thanks Ill check this out. Sorry for being so unclear