Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Can't properly use custom reward

Discussion in 'AI & Navigation Previews' started by Noxalus, Jul 27, 2020.

  1. Noxalus


    Jan 9, 2018
    Hello everyone,

    I'm trying to use AI planner custom reward for the first time, and it doesn't work as expected for me.

    The use case is simple, I have a Client (trait) with these fields:


    To simplify, a Client has only 2 actions, it can:
    - Plays (during X ticks)
    - Stops to play

    My goal here is to increase the chance for the Client to stop to play according to the time he already played.

    Reading the documentation, the proper way to do that is using a custom reward for the StopToPlay that checks the PlayTicks property of the Client. (but maybe I'm on the wrong track, please tell me if there is another way)

    So here is my custom reward struct:

    Code (CSharp):
    1. public struct StopToPlayReward : ICustomActionReward<StateData>
    2. {
    3.     public float RewardModifier(StateData originalState, ActionKey action, StateData newState)
    4.     {
    5.         TraitBasedObjectId clientId = newState.GetTraitBasedObjectId(action[0]);
    6.         TraitBasedObject client = newState.GetTraitBasedObject(clientId);
    7.         Client clientTrait = newState.GetTraitOnObject<Client>(client);
    9.         if (clientTrait.PlayTicks == 0)
    10.             return -1;
    12.         return clientTrait.PlayTicks / 100f;
    13.     }
    14. }
    And here is the action to stop to play:


    FYI, the action to play has a reward of 5.

    But in game, the value to go to the "Stop to Play" action never change.


    Did I do something wrong? Is that how it should be used? When are these reward modifiers called?

    Thank you in advance for your help!
  2. TrevorUnity


    Unity Technologies

    Jun 28, 2017
    When you added the custom reward, did you regenerate your action code?