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. The 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
  3. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here:
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

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?