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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AI See Player

Discussion in 'Scripting' started by TianWolf, Jul 12, 2012.

  1. TianWolf

    TianWolf

    Joined:
    Aug 20, 2011
    Posts:
    161
    I'm making an ai behavior where the agent will attack the player when it "sees" him. My question is what is the best way to make the agent see.
     
  2. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Test for proximity and direction, if close enough and in the right direction fire a few rays out to check if anything is in the way.
     
    Last edited: Jul 12, 2012
  3. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I would use a collider to detect if the player is in range, then either:
    A) Start sending out raycasts directly forward of the enemy's head until you see the player. You could also use a SphereCast to simulate a cone of vision / field of view.

    B) Do a simple angle compare until the angle between the enemy forward and the vector to the player is within the cone of vision you want to use, such as +/-10 degrees.
    I can't resist a shameless plug. TargetPRO can do this work with a couple of components:
    http://forum.unity3d.com/threads/13...-logical-Games?p=953417&viewfull=1#post953417
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Yeah collider is fine for the first step too, better in many ways (slightly less efficient, but much simpler/cleaner code).
     
  5. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    I would think it is more efficient since it is passive when the player isn't in range.
     
  6. TianWolf

    TianWolf

    Joined:
    Aug 20, 2011
    Posts:
    161
    Okay, that sounds like it'll be simple to do, thanks.

    The agent will be able to hear as well could that stand in for the first step?
     
    Last edited: Jul 12, 2012
  7. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    And how do you think the physics engine determines if something is in range? Magic? :p

    EDIT: Of course there are many optimisations (e.g.sleep), but in the end the physics engine is still going to have more effect on frame rate than a single distance check done each frame.

    But anyway, I agree your solution is better, its what I use in my own turret defence game!
     
    Last edited: Jul 12, 2012
  8. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    That is up to you and how you want to implement it. You could add a child object that has a smaller collider for the hearing range, and another child with a larger collider for the visual range, I suppose. You could use a simple OnTrigger in each child's script to notify the parent when it is time to act.
     
  9. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Yes :)

    Triggers are supposed to be very light. I'd love to see some tests, but honestly i have never seen it impact performance and I have a lot running at once: turrets, projectiles, enemies, etc.
     
  10. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Not likely to be any real issue but compare it to a vector sqrMagnitude calculation.You can do several million of these in less than second on an old PC.

    Edit: A very rough test ...

    5000 enemies (cubes, non kinematic rigidbodies, on a layer that doesn't collide with itself and spread out on a 50x50 area), 1 player (sphere collider with trigger radius 10), enemies drift randomly. FPS on my machine ~5m/s

    Remove all physics components and loop through each enemy doing Vector.sqrMagnitude on the difference of positions ... ~80fps.

    Do no calculation ... ~80fps
     
    Last edited: Jul 12, 2012
  11. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    Interesting. Wouldn't it be more accurate to test with isKinematic (rigid body) and isTrigger (collider) set to true? This should be the lightest use of the Physics engine.

    Thanks for taking the time to have a cursory look. I wish I had it!
     
  12. KzD

    KzD

    Joined:
    Dec 31, 2011
    Posts:
    52
    Anyone framiliar with Rain One AI?
    I am working on a FPS and am
    Looking for someone to assist me with setting it up.
    I'm brand new to the world of scripting and don't really know where to start
     
  13. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,039
    Sorry the bodies were *kinematic*, not non-kinematic.

    I'm not sure making the other colliders triggers would have much effect on performance (as they don't collide with each other), but will check it out.

    - Johnny A