Reputation: 54268
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)
, which is the position of Imported animation. 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.
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 ?
Upvotes: 3
Views: 3095
Reputation: 1
There are two ways to handle the problem. First you enable 'Apply Root Motion' in Animator panel. In this case the rotation and position data will be additive not absolute. But it might be happen that the original rotation or position will be loss after repeating your animation. In this case you can make an empty gameObject. Position it to your gameObject (into it's origin) what is animated. After this put your animated gameObject into the empty gameObject (in Hierachy view)
Upvotes: 0
Reputation: 54268
I found out the the position keyframes of FBX animations affect the position. After removed the Properties in Animation panel, the issue is gone.
However, the keyframes sometimes re-appear.
Upvotes: 1
Reputation: 2618
In my experience, it's best to make your 3D model a child object of some parent gameobject. The parent gameobject has the controller scripts and whatnot. I don't know if that will fix the issue. I'm guessing that the animation is changing the position and location of the model, which is affecting the gameobject's transform. If you make it a child of some other gameobject, that might not happen.
Upvotes: 0