While struggling with my other MecAnim problem Animation not the same each time. 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:
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.
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?
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.
Animator anim;
public float startTime;
public float endTime;
public GameObject chestTarget;
public GameObject handtarget;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("JumpUp"))
{
MatchTargetR();
Aim();
}
}
void MatchTargetR()
{
anim.MatchTarget(handtarget.transform.position, handtarget.transform.rotation, AvatarTarget.RightHand,
new MatchTargetWeightMask(Vector3.one, 0), startTime, endTime);
}
void Aim()
{
if (Vector3.Distance(chestTarget.transform.position, transform.position) < 100)
{
direction = chestTarget.transform.position - transform.position;
direction.y = 0;
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction), 5f);
}
}
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.
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.
I use a chestTarget to make sure the character is always facing the direction of the ledge while climbing.
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/d__7:MoveNext () (at Assets/PolygonFantasyHeroCharacters/Scripts/Parkour System/ParkourController.cs:87)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)