My character position is moved after an animation is assigned.
The character GameObject
is assigned with Animation
, RigidBody
, Capsule Collider
and a C# Script
Component.
The C# Script (animation part) is as follow:
public class MyCharacter : MonoBehaviour {
// Animations
public AnimationClip idleAnimation;
public AnimationClip slideAnimation;
public AnimationClip runAnimation;
public AnimationClip jumpPoseAnimation;
private Animation charAnimation;
enum CharacterState {
Idle = 0,
Running = 1,
Sliding = 2,
Jumping = 3,
}
private CharacterState charState;
void Start () {
charState = CharacterState.Running;
charAnimation = gameObject.GetComponent<Animation>();
if(!charAnimation) {
Debug.Log("No Animation Component.");
} else {
AnimationEvent syncRunEvent = new AnimationEvent();
syncRunEvent.time = runAnimation.length;
syncRunEvent.functionName = "SyncRunAnimation";
runAnimation.AddEvent(syncRunEvent);
}
}
void Update () {
// Animations
if(charAnimation) {
if(charState == CharacterState.Jumping) {
charAnimation.CrossFade(jumpPoseAnimation.name);
} else if(charState == CharacterState.Sliding) {
charAnimation.CrossFade(slideAnimation.name);
} else {
charAnimation.CrossFade(runAnimation.name);
}
}
}
void SyncRunAnimation() {
Debug.Log("Sync Run");
transform.position += new Vector3(0, 3, 0);
}
}
Before assigning the animation, the character is standing in the ground at position (0, 0.6, 0)
. After assigning, the character submerges INTO the ground, stuck; position changed to (0, -0.75, -0.55)
. How can I control the animation position ?
Note: I tried to use SyncEvent
to move up ( increase Y axis ), but it does NO effect at all.
UPDATE This only happens when Animation is set to Legacy. If I set the animation to Generic, it has no problem, but Console issues errors :
The AnimationClip ‘run’ used by the Animation component ‘MyCharacter’ must be marked as Legacy.
and the animation could not be played. How can I make it work with Generic animation / correct position with Legacy animation ?
UPDATE 2 Here is a simplified version of the scene, WITHOUT any script.
Before I hit the “PLAY” button
[22291-screen+shot+2014-02-17+at+11.01.49+am.png|22291]
After I hit the “PLAY” button
[22293-screen+shot+2014-02-17+at+11.02.13+am.png|22293]
So the problem is not related to codes, right ?