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

See if object is in field of view and how far from the center of the screen

Discussion in 'Scripting' started by TRALLALAL, Jul 16, 2014.

  1. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    I'm coding an AI and I want them to take cover based on a calculated risk.
    Every AI has a raycast at the center of their head, I want to see how far the selected cover is from that center and if it is in the field of view, is it possible?

    EDIT: One more question.

    When the AI is walking to a waypoint and it sees the player its rotation gets messed up and it keeps rotating, how can I prevent that? I'm using LookAt at the moment but can I fix this by using transform.rotation? If yes how?
     
    Last edited: Jul 16, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Uhm it's not working I think, it's returning a value based on the distance

    Code (CSharp):
    1. Vector3 targetDirection = target.transform.position - transform.position;
    2.         float angle = Vector3.Angle(targetDirection, Vector3.forward);
    3.         Debug.Log(angle);
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The dot product of the agent's normalized forward vector and the normalized vector of the agent to the cover point will tell you how much he is "facing" the cover point - essentially telling you if it's front of him, next to him, or behind him.
     
  5. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Could you explain that in simple words? :p
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  7. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    It works fine but there's still a problem, I tested it on the player and when the object is behind me the value is > 0, when I'm looking at his side and it's not in the fov it's lower than 0.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    A value less than 0 means that it is behind him. A value of exactly 0 means that it is perpendicular to him. You'll have to decide what your FOV is and test against a value within the -1 to 1 range that represents that value. There is a diagram in the Vector Cookbook in the manual that gives you some of those values. You can probably find it in wiki entries for Dot Products as well.
     
  9. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    I still can't get it to work :( even tried raycast
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're going to have to be more specific than that.
     
  11. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Values are not correct, if the object is in front of the AI sometimes it's negative, and if it's behind him it's negative as well.

    This is the dot product way, not sure if it is correct though
    Code (CSharp):
    1. Vector3 objectNormalized = Vector3.Normalize(transform.position);
    2. Vector3 targetNormalized = Vector3.Normalize(target.position);
    3. Vector3 forward = transform.TransformDirection(transform.forward);
    4. Vector3 pos = targetNormalized - objectNormalized.position;
    EDIT: I inverted the pos values(updated the code) and now it's negative when the object is behind him but still when it's on his side and he can't see it the value is positive.
     
  12. malu

    malu

    Joined:
    Jun 26, 2013
    Posts:
    6
    If a your actor is heading another target u can take a look at andeeees post:
    http://forum.unity3d.com/threads/raycasting-a-cone-instead-of-single-ray.39426/#post-253511x

    Code (CSharp):
    1.  
    2. private bool IsHeading(Vector3 pTargetPosition)
    3.  
    4.         {
    5. var direction = pTargetPosition - transform.localPosition;
    6.            var headingN = pDirection.normalized;
    7.             var sightN = transform.forward.normalized;
    8.             var dot = Vector3.Dot(headingN, sightN);
    9.             return (dot > 0.5f && dot <= 1.0f);
    10.         }
    11.  
    By the way I wouldn't raycast for coverObstacles permanently. Hold a List of coverObstacle transforms and ask them via lazy update for the nearest e.g. top 5 obstacles via sort by actor position (just use the magnitude). Then run through those until a raycast hits one of them.
     
  13. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Alright, I will try later. I'm trying to fix the field of view at the moment.
     
  14. malu

    malu

    Joined:
    Jun 26, 2013
    Posts:
    6
    yep, just use the method above.
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Er - that's not how you calculate directions :)

    Code (csharp):
    1.  
    2. Vector3 toTarget = (coverObject.transform.position - agent.transform.position).normalized;
    3. if (Vector3.Dot(agent.transform.forward, toTarget) > 0)
    4. {
    5.     // cover object is in front of agent
    6. }
    7. else
    8. {
    9.     // cover object is behind agent
    10. }
    11.  
     
  16. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Oh, it's fine now, thanks :p