Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can not get MatchTarget to work

Discussion in 'Animation' started by MikeUpchat, Jan 26, 2018.

  1. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    While struggling with my other MecAnim problem https://forum.unity.com/threads/animation-not-the-same-each-time.514373/ I have been trying to get MatchTarget to work to solve my problem of my character not ending up in the correct position while sitting even though the animation is started from the exact same point and rotation each time. So I tried to use MatchTarget but it just doesn't work for me, most of the time isMatchingTarget returns false, I have to have odd values on start time and end time to even get isMatchingTarget to return true and no matter what range of time values I use I can only get the match target to work for part of the sitting animation then isMatchingTarget returns false and the character snaps to some strange position and rotation. The call I make is:
    Code (CSharp):
    1. m_Animator.MatchTarget(seat.position, seat.rotation, AvatarTarget.Root, new MatchTargetWeightMask(Vector3.one, 1.0f), 0.0f, 1.0f);
    Which I would assume means start matching the target from when the sitting animation is playing and then reach the target position and rotation when the sitting animation completes, but it just doesn't work. I have read in other posts that Transitions just break MatchTarget which I find rather surprising, and also MatchTarget doesn't work with OnAnimatorMove (which I am using) which again I find amazing. I am really hoping that someone can point out the stupid mistake I have made so I can finally move on after a week of trying to just get my character to a repeatable position after an animation. Or if anyone knows another way to do this to stop me from going crazy.
     
  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Cry for help to Unity, are there more docs or examples that show how this works, or some list of limitations on it as I can still not get it to work after spending all weekend trying to do so?
     
  3. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Im not a coder so I can't help much - but these might help

     
  4. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,056
    Many thanks for those, hopefully that will show up what I am doing wrong with this.
     
  5. DaveDaGuy

    DaveDaGuy

    Joined:
    Apr 19, 2018
    Posts:
    2

    Hi MikeUpchat,

    I have been working with matchtarget over the past few weeks and have figured out a way to make sure it points the right way.


    Code (CSharp):
    1.  
    2.  
    3. Animator anim;
    4. public float startTime;
    5. public float endTime;
    6. public GameObject chestTarget;
    7. public GameObject handtarget;
    8.  
    9. void Start()
    10.     {
    11.          anim = GetComponent<Animator>();
    12.     }
    13.  
    14. void Update()
    15.     {              
    16.  
    17.         if (anim.GetCurrentAnimatorStateInfo(0).IsName("JumpUp"))
    18.         {
    19.             MatchTargetR();            
    20.             Aim();            
    21.         }
    22.     }
    23.  
    24. void MatchTargetR()
    25.  
    26.     {      
    27.         anim.MatchTarget(handtarget.transform.position, handtarget.transform.rotation, AvatarTarget.RightHand,
    28.                                              new MatchTargetWeightMask(Vector3.one, 0), startTime, endTime);
    29.     }
    30.  
    31.  
    32. void Aim()
    33.     {
    34.         if (Vector3.Distance(chestTarget.transform.position, transform.position) < 100)
    35.         {
    36.             direction = chestTarget.transform.position - transform.position;
    37.             direction.y = 0;
    38.  
    39.             transform.rotation = Quaternion.Slerp(transform.rotation,
    40.                                           Quaternion.LookRotation(direction), 5f);
    41.         }
    42.     }
    43.          

    So depending on what you are wanting to do the feet and hands are better things for target-matching to begin with, because the they can fix and turn the character the way they need to be in an easier way.

    Okay about the startTime and endTime, They are used to determine when it can start matching the new position.
    So the point of contact you want for the character to let's say grab the ledge, you want that to be the point in the animation where it would have contact.
    Matching_Target_00.PNG

    So in the animation scrubbing window slide to the point in the animation that it takes place it will be in a percentage.
    Take this number and move the decimal over 2 numbers to the left.
    Matching_Target_01.PNG


    I use a chestTarget to make sure the character is always facing the direction of the ledge while climbing. Matching_Target_02.PNG



    **Note** This is an example code.
     
    trombonaut and craigjwhitmore like this.
  6. craigjwhitmore

    craigjwhitmore

    Joined:
    Apr 15, 2018
    Posts:
    135
    This is better than the unity documentation!
     
  7. Bahaa_Soliman_97

    Bahaa_Soliman_97

    Joined:
    May 24, 2016
    Posts:
    3
    Well... now I'm stuck here as well, getting this error (and happy new year to everyone). If anyone knows why MatchTarget doesn't work during transitions, PLEASE let me know how to fix this... I'm following a Udemy tutorial, and the lecturer isn't getting the same error as I do:


    Calling Animator.MatchTarget while in transition does not have any effect.
    UnityEngine.Animator:MatchTarget (UnityEngine.Vector3,UnityEngine.Quaternion,UnityEngine.AvatarTarget,UnityEngine.MatchTargetWeightMask,single,single)
    ParkourController:MatchTarget (ParkourAction) (at Assets/PolygonFantasyHeroCharacters/Scripts/Parkour System/ParkourController.cs:107)
    ParkourController/<DoParkourAction>d__7:MoveNext () (at Assets/PolygonFantasyHeroCharacters/Scripts/Parkour System/ParkourController.cs:87)
    UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
     
    MateAndor likes this.
  8. MateAndor

    MateAndor

    Joined:
    Nov 5, 2014
    Posts:
    70
    Hello,

    you can fixed the Matchtarget in transition problem?
    Im working on a project and I have the save problem :(
     
  9. sophiacruzht

    sophiacruzht

    Joined:
    Jan 19, 2024
    Posts:
    1
    I loved to learning about coding and i get a lot of new things from here thanks for sharing this post.