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

Player is not moving left/right. only backword and forward

Discussion in 'ML-Agents' started by Shachartz0, May 11, 2021.

  1. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    Hi all,
    I'm trying to implement an agent that navigates in a maze.
    Im using a character controller. When using heuristic methods I press A snd D and the agent doesn't move. it only moves backward and forwards using W and S.
    I am attaching both scripts

    thank you
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SimpleCharacterController : MonoBehaviour
    6. {
    7.     [Tooltip("Move speed in meters/second")]
    8.     public float moveSpeed = 2f;
    9.  
    10.     public float ForwardInput { get; set; }
    11.     public float RLInput { get; set; }
    12.  
    13.     new private Rigidbody rigidbody;
    14.     private CapsuleCollider capsuleCollider;
    15.     private void Awake()
    16.     {
    17.         rigidbody = GetComponent<Rigidbody>();
    18.         capsuleCollider = GetComponent<CapsuleCollider>();
    19.     }
    20.     private void FixedUpdate()
    21.     {
    22.         ProcessActions();
    23.     }
    24.  
    25.     private void ProcessActions()
    26.     {
    27.         // Movement
    28.  
    29.         Vector3 move1 = Vector3.right * Mathf.Clamp(RLInput, -1f, 1f) *
    30.             moveSpeed * Time.fixedDeltaTime;
    31.         rigidbody.MovePosition(transform.position + move1);
    32.  
    33.         Vector3 move = Vector3.forward * Mathf.Clamp(ForwardInput, -1f, 1f) *
    34.             moveSpeed * Time.fixedDeltaTime;
    35.         rigidbody.MovePosition(transform.position + move);
    36.  
    37.     }
    38. }
    Code (CSharp):
    1. using Unity.MLAgents;
    2. using UnityEngine;
    3. using Unity.MLAgents.Actuators;
    4. using System.Collections;
    5.  
    6.  
    7. public class SimpleCollectorAgent : Agent
    8. {
    9.     [SerializeField]
    10.     private GameObject target;
    11.  
    12.     [SerializeField]
    13.     private Material successMateial;
    14.  
    15.     [SerializeField]
    16.     private Material failMateial;
    17.  
    18.     [SerializeField]
    19.     private Material defaultMateial;
    20.  
    21.     [SerializeField]
    22.     private MeshRenderer groundMeshRenderer;
    23.  
    24.     private SimpleCharacterController characterController;
    25.     new private Rigidbody rigidbody;
    26.     private Vector3 originalPosition;
    27.     private Vector3 originalTargetPosition;
    28.  
    29.     /// <summary>
    30.     /// Called once when the agent is first initialized
    31.     /// </summary>
    32.     public override void Initialize()
    33.     {
    34.         characterController = GetComponent<SimpleCharacterController>();
    35.         rigidbody = GetComponent<Rigidbody>();
    36.         originalPosition = transform.localPosition;
    37.         originalTargetPosition = target.transform.localPosition;
    38.     }
    39.  
    40.     /// <summary>
    41.     /// Called every time an episode begins. This is where we reset the challenge.
    42.     /// </summary>
    43.     public override void OnEpisodeBegin()
    44.     {
    45.         // Reset agent position, rotation
    46.         transform.localPosition = originalPosition;
    47. // transform.rotation = Quaternion.Euler(Vector3.up * Random.Range(0f, 360f));
    48.    //     rigidbody.velocity = Vector3.zero;
    49.  
    50.         target.transform.localPosition = originalTargetPosition;
    51.         // Reset target position
    52.         while (true)
    53.         {
    54.             target.transform.localPosition = new Vector3(Random.Range(-22.5f, 24.5f), 0.5f, Random.Range(-24f, 23.5f));
    55.             if (!(Physics.CheckSphere(target.transform.position, 0.1f)))
    56.             {
    57.                 break;
    58.             }
    59.         }
    60.     }
    61.  
    62.     /// <summary>
    63.     /// Controls the agent with human input
    64.     /// </summary>
    65.     /// <param name="actionsOut">The actions parsed from keyboard input</param>
    66.     public override void Heuristic(in ActionBuffers actionsOut)
    67.     {
    68.         // Read input values and round them. GetAxisRaw works better in this case
    69.         // because of the DecisionRequester, which only gets new decisions periodically.
    70.         int vertical = Mathf.RoundToInt(Input.GetAxisRaw("Vertical"));
    71.         int horizontal = Mathf.RoundToInt(Input.GetAxisRaw("Horizontal"));
    72.  
    73.         // Convert the actions to Discrete choices (0, 1, 2)
    74.         ActionSegment<int> actions = actionsOut.DiscreteActions;
    75.         actions[0] = vertical >= 0 ? vertical : 2;
    76.         actions[1] = horizontal >= 0 ? horizontal : 2;
    77.     }
    78.  
    79.     /// <summary>
    80.     /// React to actions coming from either the neural net or human input
    81.     /// </summary>
    82.     /// <param name="actions">The actions received</param>
    83.     public override void OnActionReceived(ActionBuffers actions)
    84.     {
    85.  
    86.         // Convert actions from Discrete (0, 1, 2) to expected input values (-1, 0, +1)
    87.         // of the character controller
    88.         float vertical = actions.DiscreteActions[0] <= 1 ? actions.DiscreteActions[0] : -1;
    89.         float horizontal = actions.DiscreteActions[1] <= 1 ? actions.DiscreteActions[1] : -1;
    90.  
    91.         characterController.ForwardInput = vertical;
    92.         characterController.RLInput = horizontal;
    93.     }
    94.     /// <summary>
    95.     /// Respond to entering a trigger collider
    96.     /// </summary>
    97.     /// <param name="other">The object (with trigger collider) that was touched</param>
    98.     private void OnTriggerEnter(Collider other)
    99.     {
    100.         // If the other object is a collectible, reward and end episode
    101.         if (other.tag == "collectible")
    102.         {
    103.             AddReward(1f);
    104.             EndEpisode();
    105.             StartCoroutine(swapGroundMaterial(successMateial, 0.5f));
    106.  
    107.         }
    108.         else if (other.tag == "walls")
    109.         {
    110.             AddReward(-.5f);
    111.         }
    112.     }
    113.     private IEnumerator swapGroundMaterial(Material mat, float time)
    114.     {
    115.         groundMeshRenderer.material = mat;
    116.         yield return new WaitForSeconds(time);
    117.         groundMeshRenderer.material = defaultMateial;
    118.  
    119.     }
    120. }
     
  2. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    What do you see when you print the value of horizontal in your Heuristic method?
     
  3. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    it is zero for some reason even when I press the right&left arrows.
     
  4. ditlevrisdahl

    ditlevrisdahl

    Joined:
    May 30, 2017
    Posts:
    26
    Under your Behavior Parameters, does "discrete actions" have branch_size 3 for the 2 actions which move you up/down and left/right?
     
  5. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    Can you make sure that your "Horizontal" axis is set up correctly to map to the keys you expect. Could you also check to see what the raw value is that is being returned from Input.GetAxisRaw("Horizontal")?
     
  6. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    yes
     
  7. Shachartz0

    Shachartz0

    Joined:
    Dec 28, 2020
    Posts:
    13
    the raw value is indeed 0..

    the horizontal axis has the same set-up as the vertical one...
     
    Last edited: Jun 14, 2021
  8. christophergoy

    christophergoy

    Unity Technologies

    Joined:
    Sep 16, 2015
    Posts:
    735
    When you say "set-up the same" do you mean it's mapped to the same keys?