I have a problem, MatchTarget works fine in Mecanim Tutorial, but not in my script. Here’s my Script:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Rigidbody), typeof (CapsuleCollider))]
public class BasicControll : MonoBehaviour {
private float direction;
private float speed;
private bool run;
private bool jump;
private Animator animator;
//This function finds nearest GameObject
GameObject FindClosest(string tag) {
GameObject[] Objects;
Objects = GameObject.FindGameObjectsWithTag(tag);
GameObject closest=new GameObject();
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject Obj in Objects) {
Vector3 diff = Obj.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = Obj;
distance = curDistance;
}
}
return closest;
}
void Start(){
animator = GetComponent <Animator>();
}
void Update(){
if (animator) {
AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo (0);
run = Input.GetKey (KeyCode.LeftShift);
jump = Input.GetKey (KeyCode.Space);
animator.SetBool ("Run", run);
animator.SetBool ("Jump", jump);
if (state.IsName ("Base Layer.JumpOn")) {
Transform RightHand=FindClosest("Jump").transform.FindChild("RightHand");
animator.MatchTarget(RightHand.position, RightHand.rotation, AvatarTarget.RightHand, new MatchTargetWeightMask(new Vector3(1, 1, 1), 0), 0.190f, 0.300f);
}
}
}
void FixedUpdate () {
direction = Input.GetAxis ("Horizontal");
speed = Input.GetAxis ("Vertical");
animator.SetFloat("Direction",direction, 0.25f, Time.deltaTime);
animator.SetFloat("Speed",speed);
}
}
And this is tutorial script:
using UnityEngine;
using System.Collections;
public class TargetMatching : MonoBehaviour
{
private Animator animator;
public Transform RightHand;
bool hasJumped = false;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
}
void OnGUI()
{
GUILayout.Label("Make your character grab any edge!");
GUILayout.Label("Press Fire1 to trigger Jump animation");
GUILayout.Label("Added *MatchStart* and *MatchEnd* parameters to the Animator Controller");
GUILayout.Label("Added *MatchStart* and *MatchEnd* additionnal curves to the Idle_ToJumpUpHigh animation");
GUILayout.Label("*MatchStart* and *MatchEnd* tell the system when to start cheating the root using MatchTarget API");
GUILayout.Label("Added a RightHand model child of the MoveThis container, to tell where the right hand should go");
GUILayout.Label("On the update function, we call MatchTarget");
GUILayout.Label("Translate the MoveThis object and see how character's hand always reach it");
}
// Update is called once per frame
void Update ()
{
if (animator)
{
AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(0);
if (Input.GetButton("Fire1")) animator.SetBool("Jump", true);
if (state.IsName("Base Layer.JumpUp") || state.IsName("Base Layer.FullJump"))
{
animator.SetBool("Jump", false);
animator.MatchTarget(RightHand.position, RightHand.rotation, AvatarTarget.RightHand, new MatchTargetWeightMask(new Vector3(1, 1, 1), 0), 0.190f, 0.300f);
hasJumped = true;
}
if (hasJumped && state.normalizedTime > 1.2)
{
hasJumped = false;
Application.LoadLevel(0);
}
}
}
}
Also i couldn’t understand how MatchStart and MatchEnd curves work, so i replaced them with values. It worked same in example.
P.S Finding GameObject works fine