Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question AI Planner 0.2.4 Custom precondition InSightOfTarget

Discussion in 'AI & Navigation Previews' started by DrBoum, Nov 20, 2020.

  1. DrBoum


    Apr 19, 2020
    i have a few question about custom precondition and data access in general,
    im using unity 2020.1.4f1 in a project using the default unity physics.
    here is a very naive custom precondition that give an error on playtime (can use physicsscene only in main thread).
    Code (CSharp):
    2. public struct TargetInSightPrecondition : ICustomActionPrecondition<StateData> {
    3.     private static PhysicsScene physics = Physics.defaultPhysicsScene;
    4.     public bool CheckCustomPrecondition(StateData state, ActionKey action)
    5.     {
    6.         Vector3 agentPos = ???;
    7.         Vector3 targetPos =???:
    8.         var targetDir = (agentPos - targetPos).normalize;
    9.         return TargetIsInSight(agentPos, targetDir);
    10.     }
    12.     public static bool TargetIsInSight(float3 position, float3 dir)
    13.     {
    14.         return physics.Raycast(position, dir);
    15.     }
    16. }
    also the "Attack" action using that precondition:

    here the questions:
    1. how can i get the information relative to this action ? (agent data and target data being compared in the precondition)
    2. how can i know if i my agent can see a target while still using the AI planner ?
    is there any hope ?
    Thank you by advance !

    Attached Files:

    Noxalus likes this.
  2. vx4


    Dec 11, 2012
  3. vx4


    Dec 11, 2012
    The code is not compatible with job system,You can use physicsscene in main thread only.
  4. Homolus


    Dec 21, 2020

    I will probably need something similar in a near future, and I don't really actually know how to do that for the moment.

    But I can help you with what I know from now.

    First thing, you have to keep in mind that pre-condition are executed when the AI planner is building the decision graph, so it's used to determine in advance if the action will be possible. That's why the raycast must be able to be done on a possible future state of your world. Even if your code was compatible with job system, I don't think the PhysicsScene will actually represent the world in the state the AI Planner actually knows.

    To get the position of both actors, you have to use their Location trait:

    Code (CSharp):
    1. public struct TargetInSightPrecondition : ICustomActionPrecondition<StateData> {
    2.     private static PhysicsScene physics = Physics.defaultPhysicsScene;
    3.     public bool CheckCustomPrecondition(StateData state, ActionKey action)
    4.     {
    5.         Location agentLocation = state.GetTraitOnObject<Location>(state.GetTraitBasedObject(state.GetTraitBasedObjectId(action[0])));
    6.         Location targetLocation = state.GetTraitOnObject<Location>(state.GetTraitBasedObject(state.GetTraitBasedObjectId(action[1])));
    8.         return ProximityUtility.IsNear(moverLocation, targetLocation);
    9.         Vector3 agentPos = agentLocation.Position;
    10.         Vector3 targetPos = targetLocation.Position:
    11.         var targetDir = (agentPos - targetPos).normalize;
    12.         return TargetIsInSight(agentPos, targetDir);
    13.     }
    14.     public static bool TargetIsInSight(float3 position, float3 dir)
    15.     {
    16.         return physics.Raycast(position, dir);
    17.     }
    18. }
    But as said before, your TargetIsInSight function must rely on the world state as seen by the AI Planner.

    The solution could be to have a trait for physics data (radius, shape, etc...) for both parameters and re-compute the raycast from that. One thing is certain: all your calculations must rely on trait data.
    vx4 likes this.