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. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

cant get custom traits to work

Discussion in 'AI & Navigation Previews' started by Vetleskag, Apr 1, 2020.

  1. Vetleskag

    Vetleskag

    Joined:
    Oct 18, 2012
    Posts:
    3
    I have a problem when it comes to creating custom traits. I have followed what information I could find on the forums and in the documentation, yet when I generate the assemblies used for the AI my custom traits are not used/found.

    I have made a script inheriting ICustomTrait and IEquatable, placed it in the namespace AI.Planner.Domains(also tried Unity.AI.Planner.DomainLanguage.TraitBased and AI.Planner.Custom.mynamespace) and I've made a assembly definition file referencing the necessary assemblies, as well as a trait asset in sync with the trait script in the same folder as the custom trait.

    My script is essentially the same as the location trait as I've narrowed it down just to get it working, yet when generating the ai code the generator always creates a new trait. I'm sure I am missing something here but I can't see what..

    My asmdef:
    Unity_sQQdQKKyQX.png


    My custom trait, copy of location:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using Unity.AI.Planner.DomainLanguage.TraitBased;
    5.  
    6. namespace AI.Planner.Domains
    7. {
    8.     [Serializable]
    9.     public struct InRangeLocation : ICustomTrait, IEquatable<InRangeLocation>
    10.     {
    11.  
    12.         public int transformInstanceId;
    13.         public Vector3 position;
    14.         public Vector3 forward;
    15.         public float range;
    16.  
    17.  
    18.  
    19.         public Transform Transform
    20.         {
    21.             get => null;
    22.             set
    23.             {
    24.                 transformInstanceId = value.GetInstanceID();
    25.                 position = value.position;
    26.                 forward = value.forward;  
    27.             }
    28.         }
    29.  
    30.  
    31.         public bool Equals(InRangeLocation other)
    32.         {
    33.             return transformInstanceId.Equals(other.transformInstanceId)
    34.                && position == other.position
    35.                && forward == other.forward;
    36.         }
    37.  
    38.         public object GetField(string fieldName)
    39.         {
    40.             switch (fieldName)
    41.             {
    42.                 case nameof(position):
    43.                     return position;
    44.                 case nameof(forward):
    45.                     return forward;
    46.                 case nameof(range):
    47.                     return range;
    48.             }
    49.             return null;
    50.         }
    51.  
    52.         public void SetField(string fieldName, object value)
    53.         {
    54.             switch (fieldName)
    55.             {
    56.                 case nameof(position):
    57.                     position = (Vector3)value;
    58.                     break;
    59.                 case nameof(forward):
    60.                     forward = (Vector3)value;
    61.                     break;
    62.                 case nameof(range):
    63.                     range = (float)value;
    64.                     break;
    65.  
    66.  
    67.                 case nameof(Transform):
    68.                     Transform = (Transform)value;
    69.                     break;
    70.  
    71.             }
    72.         }
    73.  
    74.         public override string ToString()
    75.         {
    76.             return $"Location: {position} {forward} {range}";
    77.         }
    78.     }
    79.  
    80. }

    Unity 2019.3.1f1
    AI-planner 0.2.2
     
  2. TrevorUnity

    TrevorUnity

    Unity Technologies

    Joined:
    Jun 28, 2017
    Posts:
    118
    Have you added a trait definition for the custom trait? That's how the package knows which traits to include, so I suspect that's what's missing.
     
  3. Vetleskag

    Vetleskag

    Joined:
    Oct 18, 2012
    Posts:
    3
    Yeah, I've got a trait definition asset in sync with the customtrait script, even set the namespace field to my customtrait namespace. The problem is that the code generator fails to recognize the customtrait script and always generates a new trait script in the domains assembly instead of using my custom one. Am I missing something in the namespace/assembly setup?
     
    Last edited: Apr 8, 2020
  4. mplantady_unity

    mplantady_unity

    Unity Technologies

    Joined:
    Jun 19, 2019
    Posts:
    49
    So there is an error in the documentation about namespace/assembly for ICustomTrait.

    For now the only option to create one is to:
    Create an AssemblyDefinitionReference (AsmRef) pointing to Unity.AI.Planner assembly.

    Create your ICustomTrait type in the same folder of the AsmRef and with the same name than your Trait asset and put it in Unity.AI.Planner.DomainLanguage.TraitBased' namespace.
     
  5. Vetleskag

    Vetleskag

    Joined:
    Oct 18, 2012
    Posts:
    3
    Haven't looked too closely at it yet, but at first glance it looks like it's working now, cheers!
     
  6. keeperman

    keeperman

    Joined:
    Aug 3, 2013
    Posts:
    9
    Hi there, I'm not sure if this is the right place for this but, when following these steps the Unity.AI.Planner library reference gets overwritten by a .csproj file which breaks Visual Studio 2019's reference pathing
     
  7. TrevorUnity

    TrevorUnity

    Unity Technologies

    Joined:
    Jun 28, 2017
    Posts:
    118
    That's not good. Multiple of the team develop with Rider, so we haven't experienced this potential issue with Visual Studio. Can you provide the exact steps you took so we can reproduce this error?
     
  8. keeperman

    keeperman

    Joined:
    Aug 3, 2013
    Posts:
    9
    Okay sure. What I did was I created a new folder under the assets directory, in which I created the AsmRef file pointing to the Unity.AI.Planner assembly. Along with a ICustomTrait class and its twin asset ( As shown above. ). Which is working fine in Unity but the AsmRef creates the .csproj which Visual prefers over the actual assembly.

    A fix for this is to remove the new fake assembly reference and add the real one in "\Library\ScriptAssemblies"

    Edit: Words
     
    Last edited: Apr 26, 2020
    Dimn721 likes this.
  9. mplantady_unity

    mplantady_unity

    Unity Technologies

    Joined:
    Jun 19, 2019
    Posts:
    49
    What version of Unity are you using for this project ?
     
  10. keeperman

    keeperman

    Joined:
    Aug 3, 2013
    Posts:
    9
    Sorry for the delay but I'm using version 2019.3.8f1 with Visual studio 16.5.4
     
    Last edited: May 8, 2020