- Home /
Animation issues With Game Character
Wondering if anyone can help me with this one...I have exported a skinned mesh character into unity and when i play his animations, he shrinks and snaps back to 0,0,0 position. So i put an empty game object on him so it can play its animation where i want it to play. That works and all but how do i get the character controller to work on a parented Game Character? I put the character controller on the game character and nothing happens. I have terrain collider in position for the game character to fall on and nothing happens. Can anyone please help me???
Answer by AmunTech · Dec 28, 2011 at 03:33 AM
I think what you want to do is to add the character controller on the empty gameobject and create a variable that holds the character mesh. After this create a function that play the animations, something like this:
//This is the character var Character : GameObject;
// In the update function add the AnimateCharacter function
function Update(){
AnimateCharacter();
}
//This is the function that crossfade the animations
function AnimateCharacter(){
if (Input.GetAxis("Vertical") || Input.GetAxis("Horizontal")){
Character.animation.CrossFade("walk");
}else{
Character.animation.CrossFade("idle");
}
}
EDIT private var motor : CharacterMotor; var Character : GameObject;
// Use this for initialization function Awake () { Character.animation.wrapMode = WrapMode.Loop; motor = GetComponent(CharacterMotor); }
// Update is called once per frame function Update () { // Get the input vector from kayboard or analog stick var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
AnimateCharacter();
}
// Require a character controller to be attached to the same game object @script RequireComponent (CharacterMotor) @script AddComponentMenu ("Character/FPS Input Controller")
function AnimateCharacter(){ if (Input.GetAxis("Vertical") && Input.GetAxis("Horizontal")){ Character.animation.CrossFade("Run");
}else{ Character.animation.CrossFade("idle");
}
}
private var moveDirection : Vector3 = Vector3.zero; private var speed : float = 6.0; var footballPlayer : CharacterController; var playerObject : GameObject; function Start(){
footballPlayer = GetComponent(CharacterController); playerObject.animation.wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop; } function Update () {
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal") ){ playerObject.animation["Run"].speed = 1;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
playerObject.animation.CrossFade("Run");
playerObject.animation["Run"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;
footballPlayer.$$anonymous$$ove(moveDirection * Time.deltaTime);
}else{
playerObject.animation.CrossFade("idle");
}
}
This is the code that im using and i put this on the parent object. but it still doesn't make the character controller respond on the parent object. I really don't know what to do from here.
Try to recreate it yourself and see what exactly i am talking about.
take a look at the edited script, "the character mesh have to be inside of the empty gameobject". I tested it so i can confirm that this works fine.
i'm happy to be helpful, don't forget to set the answer as correct and happy new year!