- Home /
MatchTarget unable to target and giving varried results
I tried searching for MatchTarget with varied results but their problem is a bit different than mine.
What i am trying to do is make a character jump and climb a wall. I am using MatchTarget first for its Right hand then for its right Leg after the first matching finishes. But what i am getting is sometimes after going through 90% of the animation my character suddenly disappears(when i checked, its y co-ordinate changes to something 2e+10, and increases continuously) or other times it does even match my target as if it was never called in the function.
Here is the code..
using UnityEngine; using System.Collections;
public class Controller : MonoBehaviour {
public float speed=2f;
public float angular=1.5f;
public float damping=5f;
public float angDamping=5f;
public Transform pos;
public Transform pos1;
Animator anim;
HashIds hash;
CapsuleCollider[] cols;
RaycastHit hit;
float height=2f;
int layerMask;
/You may skip this part as it only is assigning the triggers for the animator,but if u still want to, you may go ahead/
void Awake() {
anim = GetComponent<Animator> ();
hash = GameObject.FindGameObjectWithTag ("GameController").GetComponent<HashIds> ();
cols = GetComponentsInChildren<CapsuleCollider> ();
pos = GameObject.FindGameObjectWithTag ("Floor").transform;
}
void Update() {
layerMask = 1 << 8;
float v = Input.GetAxis ("Vertical");
float h = Input.GetAxis ("Horizontal");
if (Input.GetKey (KeyCode.LeftShift)) {
speed = Mathf.Lerp (2.0f, 6.0f, damping);
} else {
speed = 2f;
}
if (Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.D)) {
angular = Mathf.Lerp (1.5f, 3.0f, angDamping);
} else {
angular = 1.5f;
}
anim.SetFloat (hash.Speed, v * speed);
anim.SetFloat (hash.AngularSpeed, h * angular);
if (Input.GetKeyDown (KeyCode.Space) && speed > 5.5f) {
anim.SetTrigger (hash.RunningJump);
}
if (Input.GetKeyDown (KeyCode.LeftAlt)) {
anim.SetTrigger (hash.Dive);
}
if (transform.position.y > height) {
if (Physics.Raycast (transform.position+new Vector3(0f,0.2f,0f), -transform.up, out hit)) {
if (hit.distance > 0.5f && hit.distance <= 4f) {
if (anim.GetCurrentAnimatorStateInfo(0).shortNameHash!=Animator.StringToHash("JumpDown"))
{
anim.SetTrigger (hash.DownJump);
if(!anim.IsInTransition(0))
anim.MatchTarget (pos.position, Quaternion.identity, AvatarTarget.RightFoot, new MatchTargetWeightMask (new Vector3 (0, 1, 0), 0), 0.20f, 036f);
}
} else if (hit.distance > 4f && hit.distance <= 7f) {
anim.SetTrigger (hash.GreatHeight);
if(anim.GetCurrentAnimatorStateInfo(0).shortNameHash!=Animator.StringToHash("Dying"))
anim.SetTrigger (hash.Revive);
} else if (hit.distance > 7f) {
anim.SetTrigger (hash.GreatHeight);
}
}
if (transform.position.y > 0.5f)
height = transform.position.y;
else
height = 2f;
}
if (Input.GetKey (KeyCode.C)) {
anim.SetBool (hash.Crouch, true);
} else {
anim.SetBool (hash.Crouch, false);
}
if(!anim.GetCurrentAnimatorStateInfo(0).IsName("JumpUp") && anim.GetCurrentAnimatorStateInfo(0).shortNameHash!=Animator.StringToHash("JumpUp")) {
//GetComponent<MeshCollider>().enabled=true;
//GetComponent<BoxCollider>().enabled=true;
GetComponent<Rigidbody>().useGravity=true;
}
}
/the part where my problem begins*/
void OnTriggerStay() {
if (Input.GetKeyDown(KeyCode.Space)){
Collider[] sper=Physics.OverlapSphere(transform.position+new Vector3(0f,1.6f,0f),1.0f,layerMask);
if (sper.Length == 0)
anim.SetTrigger (hash.CannotJump);
else {
//GetComponent<CapsuleCollider>().height=anim.GetFloat("colliderYCurve");
anim.SetTrigger (hash.JumpUp);
//GetComponent<BoxCollider>().enabled=false;
//GetComponent<MeshCollider>().enabled=false;
anim.MatchTarget(pos1.position,pos1.rotation,AvatarTarget.RightHand, new MatchTargetWeightMask (new Vector3 (0, 1, 0), 0), 0.141f, 0.251f);
anim.MatchTarget(pos1.position,pos1.rotation,AvatarTarget.RightFoot, new MatchTargetWeightMask (new Vector3 (0, 1, 0), 0), 0.25f, 0.65f);
}
}
}
}
For the reference I have used several triggers(like JumpUp-for jumping up animation, crouch-for crouchig etc) in my animator and i am using root motion for controlling my character instead of a character controller and i have used a layer to use it with the overlap sphere only to detect collisions.I dont think there is anything wrong with the syntax or its use(i may be wrong).Why is this happening??