- Home /
Mecanim tute problem.
hello,In Mecanim tutorial project in the Tutorial_done scene,The character(Kyle_Robot) does not touching the ground after jumpdownroll...does anybody know how to solve this :D thank you...
Answer by munaeem · Nov 22, 2012 at 04:10 PM
Ok there is a fix to it now just Past the following Script to BotCtrl.
using UnityEngine;
using System;
using System.Collections;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class BotCtrl : MonoBehaviour {
[System.NonSerializedAttribute]
public bool gotWrench;
protected Animator animator;
CapsuleCollider col;
float h;
float v;
AnimatorStateInfo baseCurrentState;
AnimatorStateInfo baseNextState;
AnimatorStateInfo otherMovesCurrentState;
public float DirectionDampTime = 0.15f;
public float AnimSpeed = 1.2f;
public float MoveSpeed = 1.5f;
public float rotSpeed = 90.0f;
public float JumpHeight = 70.0f;
public bool useCurves;
Transform grabbableObj;
void Awake(){
if(gameObject.tag != "Player")
gameObject.tag="Player";
}
void Start () {
animator = GetComponent<Animator>();
animator.speed=AnimSpeed;
if(animator.layerCount>1){
animator.SetLayerWeight(1,1.0f);
}
col = GetComponent<CapsuleCollider>();
grabbableObj = GameObject.FindWithTag("grabbable").transform;
}
void FixedUpdate () {
// Grab Input each frame
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
if(animator){
// Set Speed and Direction Parameters to H and V axes
animator.SetFloat ("Speed", v);
animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);
// Set variables for info on states
baseCurrentState = animator.GetCurrentAnimatorStateInfo(0);
baseNextState = animator.GetNextAnimatorStateInfo(0);
if(animator.layerCount>1){
otherMovesCurrentState = animator.GetCurrentAnimatorStateInfo(1);
}
//Trigger Jump if we are running
if(baseCurrentState.IsName("Base.Locomotion")){
if(Input.GetButtonDown("Jump")){
animator.SetBool("Jump", true );
}
}
// Are we in idle state in Base Layer? set actions
else if(baseCurrentState.IsName("Base.Idle")){
// allow pivot on spot with Horizontal axis keys
transform.Rotate (new Vector3(0,h*Time.deltaTime*rotSpeed,0));
// make fire button cause a Wave during idle
if(Input.GetButtonDown("Jump")){
animator.SetBool("Wave", true );
}
}
else if(baseCurrentState.IsName("Base.idleGrab")){
// allow pivot on spot with Horizontal axis keys
transform.Rotate (new Vector3(0,h*Time.deltaTime*rotSpeed,0));
animator.SetBool("Grab", false );
}
else if(baseCurrentState.IsName("Base.Jump") && !animator.IsInTransition(0)){
// Reset our parameter to avoid looping
animator.SetBool("Jump", false);
// allow half as much rotation during jump
transform.Rotate (new Vector3(0,h*Time.deltaTime*rotSpeed*0.5f,0));
if(useCurves){
//set the collider height and Y center during the jumps, based on curves
col.center=new Vector3(0, animator.GetFloat("ColliderY"), 0);
col.height=animator.GetFloat("ColliderHeight");
//add extra force to main jump
rigidbody.AddForce(Vector3.up * animator.GetFloat("Jumper") * JumpHeight);
}
}
else if(baseCurrentState.IsName("Base.JumpDownRollRun") && !animator.IsInTransition(0)){
if(useCurves){
col.center=new Vector3(0,animator.GetFloat("ColliderY"),0);
col.height=animator.GetFloat("ColliderHeight");
}
}
else{
// fallback to reset collider height & position
col.center = new Vector3(0,1,0);
col.height=2;
}
// Check other moves layer for status
if(otherMovesCurrentState.IsName("OtherMoves.Wave")){
animator.SetBool("Wave", false );
}
}
}
void OnAnimatorIK(){
if(baseCurrentState.IsName("Base.idleGrab") || baseNextState.IsName("Base.idleGrab")){
float grabCurve = animator.GetFloat ("GrabCurve");
AvatarIKGoal rightHand = AvatarIKGoal.RightHand;
if(grabbableObj != null && !gotWrench) {
if(useCurves){
// Use the Curve from grabbing animation to drive smoothing of avatar hand position
animator.SetIKPositionWeight(rightHand, grabCurve);
animator.SetIKRotationWeight(rightHand, grabCurve);
}
// Set IK hand position and rotation destination
animator.SetIKPosition(rightHand, grabbableObj.position);
animator.SetIKRotation(rightHand, grabbableObj.rotation);
}
else{
// Reset Hand to be controlled by animation
animator.SetIKPositionWeight(rightHand, 0);
animator.SetIKRotationWeight(rightHand, 0);
}
}
}
void OnAnimatorMove(){
// Set up for a rigidbody - set the RB position equal to the animator deltaPosition and increase by MoveSpeed
rigidbody.position += animator.deltaPosition * MoveSpeed;
transform.rotation *= animator.deltaRotation;
}
}
This is the perfect script and thanks to @Oceany for the answer.
Answer by Oceany · Nov 22, 2012 at 07:31 AM
The problem is that the released version change OnAvatarMove and OnAvatarIK to OnAnimatorMove and OnAnimatorIK respectively. Also, in the "base" state for the animation controller, you have to check the checkbox for "IK Pass". You may need to expand the base state if it's not visible.
i checked "Ik Pass" in base State,still same problem... Any more ideas :) thank u .
And you changed the function name to OnAnimator$$anonymous$$ove and OnAnimatorI$$anonymous$$? That was what fixed it for me.
Answer by Lboy · Nov 20, 2012 at 09:45 PM
have you tried adjusting the rigidbody colliders mass? Im no expert but maybe the increased mass would bring him closer to the floor? or perhaps some other adjustments regarding his collider
Answer by munaeem · Nov 20, 2012 at 10:05 PM
I had the same problem and i posted in aswell. The problem is we are using a capsule collider rather then character controller. so if we add a character controller on the player and remove the capsule collider after changing some codes from the script the player wi just fall down or while jumping won't scale the collider if used with curves. So the best solution is to wait until there is a fix to or else play around with the script because i have done everything i could to make it work and found out that it is a Unity issue. So let them solve it i would suggest .
But are you sure this explains the issue of jump not working what so ever? I havent removed ANYTHING from my character, I only followed the steps in the tut using the exact same script(s)
And I am only reffering to Jump not Jumpdownroll or the floating above the floor issue, I have also double checked the animations are named the same as they are mentioned in script
coolkid I answered this on your main question - you need to remove the extra space character you likely but in when defining the 'base layer' as "Base"