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.

Question OrientationCubeController: Explanation and Help

Discussion in 'ML-Agents' started by unity_4c4DeXtL7Cmiig, Jan 8, 2023.

  1. unity_4c4DeXtL7Cmiig

    unity_4c4DeXtL7Cmiig

    Joined:
    Nov 23, 2022
    Posts:
    1
    I am working on a project similar to the Walker Example in the Unity ML Agents Package (Agent with Controllable Joints). I have found that in the Example the Script OrientationCube Controller is used:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Unity.MLAgentsExamples
    4. {
    5.     /// <summary>
    6.     /// Utility class to allow a stable observation platform.
    7.     /// </summary>
    8.     public class OrientationCubeController : MonoBehaviour
    9.     {
    10.         //Update position and Rotation
    11.         public void UpdateOrientation(Transform rootBP, Transform target)
    12.         {
    13.             var dirVector = target.position - transform.position;
    14.             dirVector.y = 0; //flatten dir on the y. this will only work on level, uneven surfaces
    15.             var lookRot =
    16.                 dirVector == Vector3.zero
    17.                     ? Quaternion.identity
    18.                     : Quaternion.LookRotation(dirVector); //get our look rot to the target
    19.  
    20.             //UPDATE ORIENTATION CUBE POS & ROT
    21.             transform.SetPositionAndRotation(rootBP.position, lookRot);
    22.         }
    23.        
    24.     }
    25. }
    26.  
    Can someone explain to me what this code does? I reckon it serves to prevent Agents from moving orientation very quickly and eratically.

    Also, when I try to attach thid Script to a GameObject, I get the following Error Message:

    Can't add script component 'OrientationCubeController' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.

    The Filename obviously matches the Class Name because I copied the Script from the Walker Example where it was working and attached to the GameObject. My Unity Version is 2022.1.23f1 and the Script was used in an earlier Editor Version.