@@ -353,25 +353,35 @@ static private void CollectVector3Modifications(IAnimationRecordingState state,
353353 }
354354 }
355355
356- static private void ProcessVector3Modification ( IAnimationRecordingState state , EditorCurveBinding baseBinding , UndoPropertyModification modification , Transform target , string axis )
356+ static private void ProcessVector3Modification ( IAnimationRecordingState state , EditorCurveBinding baseBinding , UndoPropertyModification modification , Transform target , string axis , float scale = 1.0f )
357357 {
358358 var binding = baseBinding ;
359- var property = modification . previousValue ;
360-
361359 binding . propertyName = binding . propertyName . Remove ( binding . propertyName . Length - 1 , 1 ) + axis ;
362360
363- if ( property == null )
361+ object currentValue = CurveBindingUtility . GetCurrentValue ( state . activeRootGameObject , binding ) ;
362+
363+ var previousModification = modification . previousValue ;
364+ if ( previousModification == null )
364365 {
365366 // create dummy
366- property = new PropertyModification ( ) ;
367- property . target = target ;
368- property . propertyPath = binding . propertyName ;
369- object currentValue = CurveBindingUtility . GetCurrentValue ( state . activeRootGameObject , binding ) ;
370- property . value = ( ( float ) currentValue ) . ToString ( ) ;
367+ previousModification = new PropertyModification ( ) ;
368+ previousModification . target = target ;
369+ previousModification . propertyPath = binding . propertyName ;
370+ previousModification . value = ( ( float ) currentValue ) . ToString ( ) ;
371371 }
372372
373- state . AddPropertyModification ( binding , property , modification . keepPrefabOverride ) ;
374- AddKey ( state , binding , typeof ( float ) , property ) ;
373+ object previousValue = currentValue ;
374+ ValueFromPropertyModification ( previousModification , binding , out previousValue ) ;
375+
376+ state . AddPropertyModification ( binding , previousModification , modification . keepPrefabOverride ) ;
377+
378+ if ( scale != 1.0f )
379+ {
380+ previousValue = ( object ) ( ( float ) previousValue / scale ) ;
381+ currentValue = ( object ) ( ( float ) currentValue / scale ) ;
382+ }
383+
384+ AddKey ( state , binding , typeof ( float ) , previousValue , currentValue ) ;
375385 }
376386
377387 static public void ProcessVector3Modifications ( IAnimationRecordingState state , ref Dictionary < object , Vector3Modification > vector3Modifications )
@@ -412,8 +422,14 @@ static public void ProcessModifications(IAnimationRecordingState state, UndoProp
412422 Type type = AnimationUtility . PropertyModificationToEditorCurveBinding ( prop , root , out binding ) ;
413423 if ( type != null )
414424 {
425+ object currentValue = CurveBindingUtility . GetCurrentValue ( root , binding ) ;
426+
427+ object previousValue = null ;
428+ if ( ! ValueFromPropertyModification ( prop , binding , out previousValue ) )
429+ previousValue = currentValue ;
430+
415431 state . AddPropertyModification ( binding , prop , modifications [ i ] . keepPrefabOverride ) ;
416- AddKey ( state , binding , type , prop ) ;
432+ AddKey ( state , binding , type , previousValue , currentValue ) ;
417433 }
418434 }
419435 }
@@ -481,7 +497,7 @@ static bool ValueFromPropertyModification(PropertyModification modification, Edi
481497 }
482498 }
483499
484- static void AddKey ( IAnimationRecordingState state , EditorCurveBinding binding , Type type , PropertyModification modification )
500+ static void AddKey ( IAnimationRecordingState state , EditorCurveBinding binding , Type type , object previousValue , object currentValue )
485501 {
486502 GameObject root = state . activeRootGameObject ;
487503 AnimationClip clip = state . activeAnimationClip ;
@@ -491,23 +507,18 @@ static void AddKey(IAnimationRecordingState state, EditorCurveBinding binding, T
491507
492508 AnimationWindowCurve curve = new AnimationWindowCurve ( clip , binding , type ) ;
493509
494- // Add key at current frame
495- object currentValue = CurveBindingUtility . GetCurrentValue ( root , binding ) ;
496-
510+ // Add previous value at first frame on empty curves.
497511 if ( state . addZeroFrame )
498512 {
499513 // Is it a new curve?
500514 if ( curve . length == 0 )
501515 {
502- object oldValue = null ;
503- if ( ! ValueFromPropertyModification ( modification , binding , out oldValue ) )
504- oldValue = currentValue ;
505-
506516 if ( state . currentFrame != 0 )
507- AnimationWindowUtility . AddKeyframeToCurve ( curve , oldValue , type , AnimationKeyTime . Frame ( 0 , clip . frameRate ) ) ;
517+ AnimationWindowUtility . AddKeyframeToCurve ( curve , previousValue , type , AnimationKeyTime . Frame ( 0 , clip . frameRate ) ) ;
508518 }
509519 }
510520
521+ // Add key at current frame.
511522 AnimationWindowUtility . AddKeyframeToCurve ( curve , currentValue , type , AnimationKeyTime . Frame ( state . currentFrame , clip . frameRate ) ) ;
512523
513524 state . SaveCurve ( curve ) ;
@@ -578,17 +589,17 @@ static private void ProcessRootMotionModifications(IAnimationRecordingState stat
578589
579590 if ( m . lastP . previousValue != null )
580591 {
581- ProcessAnimatorModification ( state , animator , m . px , "MotionT.x" , position . x , scale . x ) ;
582- ProcessAnimatorModification ( state , animator , m . py , "MotionT.y" , position . y , scale . y ) ;
583- ProcessAnimatorModification ( state , animator , m . pz , "MotionT.z" , position . z , scale . z ) ;
592+ ProcessRootMotionModification ( state , animator , m . px , "MotionT.x" , position . x , scale . x ) ;
593+ ProcessRootMotionModification ( state , animator , m . py , "MotionT.y" , position . y , scale . y ) ;
594+ ProcessRootMotionModification ( state , animator , m . pz , "MotionT.z" , position . z , scale . z ) ;
584595 }
585596
586597 if ( m . lastR . previousValue != null )
587598 {
588- ProcessAnimatorModification ( state , animator , m . rx , "MotionQ.x" , rotation . x , 1 ) ;
589- ProcessAnimatorModification ( state , animator , m . ry , "MotionQ.y" , rotation . y , 1 ) ;
590- ProcessAnimatorModification ( state , animator , m . rz , "MotionQ.z" , rotation . z , 1 ) ;
591- ProcessAnimatorModification ( state , animator , m . rw , "MotionQ.w" , rotation . w , 1 ) ;
599+ ProcessRootMotionModification ( state , animator , m . rx , "MotionQ.x" , rotation . x , 1 ) ;
600+ ProcessRootMotionModification ( state , animator , m . ry , "MotionQ.y" , rotation . y , 1 ) ;
601+ ProcessRootMotionModification ( state , animator , m . rz , "MotionQ.z" , rotation . z , 1 ) ;
602+ ProcessRootMotionModification ( state , animator , m . rw , "MotionQ.w" , rotation . w , 1 ) ;
592603 }
593604 }
594605 }
@@ -607,6 +618,7 @@ static private void ProcessAnimatorModifications(IAnimationRecordingState state,
607618 bool isHuman = animator != null ? animator . isHuman : false ;
608619 bool hasRootMotion = animator != null ? animator . hasRootMotion : false ;
609620 bool applyRootMotion = animator != null ? animator . applyRootMotion : false ;
621+ bool hasRootCurves = clip . hasRootCurves ;
610622
611623 // process animator positions
612624 List < object > discardListPos = new List < object > ( ) ;
@@ -625,7 +637,7 @@ static private void ProcessAnimatorModifications(IAnimationRecordingState state,
625637 continue ;
626638
627639 bool isRootTransform = root . transform == target ;
628- bool isRootMotion = ( isHuman || hasRootMotion || applyRootMotion ) && isRootTransform ;
640+ bool isRootMotion = isRootTransform && applyRootMotion && hasRootCurves && ( isHuman || hasRootMotion ) ;
629641 bool isHumanBone = isHuman && ! isRootTransform && animator . IsBoneTransform ( target ) ;
630642
631643 if ( isHumanBone )
@@ -648,6 +660,16 @@ static private void ProcessAnimatorModifications(IAnimationRecordingState state,
648660 rootMotionModification . py = m . y ;
649661 rootMotionModification . pz = m . z ;
650662
663+ discardListPos . Add ( item . Key ) ;
664+ }
665+ else if ( applyRootMotion )
666+ {
667+ Vector3 scale = target . localScale * ( isHuman ? animator . humanScale : 1 ) ;
668+
669+ ProcessVector3Modification ( state , binding , m . x , target , "x" , scale . x ) ;
670+ ProcessVector3Modification ( state , binding , m . y , target , "y" , scale . y ) ;
671+ ProcessVector3Modification ( state , binding , m . z , target , "z" , scale . z ) ;
672+
651673 discardListPos . Add ( item . Key ) ;
652674 }
653675 }
@@ -674,7 +696,7 @@ static private void ProcessAnimatorModifications(IAnimationRecordingState state,
674696 continue ;
675697
676698 bool isRootTransform = root . transform == target ;
677- bool isRootMotion = ( isHuman || hasRootMotion || applyRootMotion ) && isRootTransform ;
699+ bool isRootMotion = isRootTransform && applyRootMotion && hasRootCurves && ( isHuman || hasRootMotion ) ;
678700 bool isHumanBone = isHuman && ! isRootTransform && animator . IsBoneTransform ( target ) ;
679701
680702 if ( isHumanBone )
@@ -740,7 +762,7 @@ static private void ProcessAnimatorModifications(IAnimationRecordingState state,
740762 ProcessRootMotionModifications ( state , ref rootMotionModifications ) ;
741763 }
742764
743- static void ProcessAnimatorModification ( IAnimationRecordingState state , Animator animator , UndoPropertyModification modification , string name , float value , float scale )
765+ static void ProcessRootMotionModification ( IAnimationRecordingState state , Animator animator , UndoPropertyModification modification , string name , float value , float scale )
744766 {
745767 AnimationClip clip = state . activeAnimationClip ;
746768
0 commit comments