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

Boolean action for mouse clicks?

Discussion in 'ML-Agents' started by Deleted User, Jun 7, 2020.

  1. Deleted User

    Deleted User

    Guest

    I am unsure how to implement an action for a bool. I have an attack with Input.GetMouseButtonDown(0) and I am unsure how to implement this in OnActionReceived(). Can I pass in a bool instead or do I need to clamp the value in the list or cast it to an int?

    Code (CSharp):
    1. public override void OnActionReceived(float[] vectorAction)
    2.     {
    3.         // Actions
    4.         Vector3 controlSignal = Vector3.zero;
    5.         controlSignal.y = vectorAction[0];
    6.         rBody.AddTorque(controlSignal * torque);
    7. //This next part I'm unsure how to code:
    8.         if (vectorAction[1])  //assuming it can be a bool
    9.         {
    10.             anim.SetTrigger("punch");
    11.         }
    12. // or maybe
    13.         if (vectorAction[1] > 0)
    14.         {
    15.             anim.SetTrigger("punch");
    16.         }
    17. //  ... reward code after this
    18.  
    I haven't worked in Unity for a little while so I have some cobwebs to dust off. Any guidance is appreciated!
     
  2. MrWetsnow

    MrWetsnow

    Joined:
    Jan 5, 2020
    Posts:
    60
    OnActionReceived() does not take, as input, a human action. It takes as input a possible action that is picked by the learning engine (random at first). So the question, as posed, doesn't really make sense. You wouldn't react to a GetMouseButtonDown() inside OnActionReceived.

    Can you please explain what you are trying to achieve?
     
  3. Deleted User

    Deleted User

    Guest

    In the code snippet I posted, what I was using to attack, is an animation trigger that gets called when the mouse is clicked. The vectorAction list will try random values to see if it can maximize the reward, and instead of a float value I was hoping to have it implement a bool. So it would either activate the animation or not. There is a reward associated with the animation where a child collides with a target and gives a reward. I think if I just check if the value of vectorAction[1] is greater than zero, this will do what I hope. I think it will keep trying float values and eventually learn that a positive value results in the animation triggering, which can lead to a reward. But I was originally hoping to pass a bool instead of a float to OnActionReceived(). I understand it's not taking in an action like GetMouseButtonDown() and I did not code it to do that. Sorry if my question is unclear, I am just a bit uncertain on if my approach is valid or if there is a "better" way, or even a correct way. I have a few other issues to iron out before I can start training so I haven't been able to fully test the functionality yet.

    Thanks!
     
  4. MrWetsnow

    MrWetsnow

    Joined:
    Jan 5, 2020
    Posts:
    60
    What is your agent in this case? What is it that you want your agent to learn to do?
     
  5. MrWetsnow

    MrWetsnow

    Joined:
    Jan 5, 2020
    Posts:
    60
    But to answer your question more directly. I you want your action input to be a boolean, you would need to make sure you are using discrete vector space with a branch size of 1 (if you have no other inputs) and a branch 0 size of 2 (true, false).

    They your input function gets a []float (of size 1) and the first element in that list is either 0 or 1. Then you simply treat 0 as false and 1 as true in your code.
     
  6. Deleted User

    Deleted User

    Guest

    Thanks! It seems to work as expected if I just use

    Code (CSharp):
    1.         if (vectorAction[1] > 0)
    2.         {
    3.             anim.SetTrigger("punch");
    4.         }
    So as long as it works, I'm happy. I was just unsure of how to properly implement it.