Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug in Tutorial Script! - (Scripting Root Motion for “in-place” humanoid animations)

Discussion in 'Documentation' started by MagicStyle, Nov 22, 2017.

  1. MagicStyle

    MagicStyle

    Joined:
    Oct 17, 2017
    Posts:
    5
    The original code in the documentation chapter
    Tutorial: Scripting Root Motion for “in-place” humanoid animations
    does not work! The player does still move in place.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Animator))]
    5.  
    6. public class RootMotionScript : MonoBehaviour {
    7.        
    8.     void OnAnimatorMove()
    9.     {
    10.             Animator animator = GetComponent<Animator>();
    11.                          
    12.             if (animator)
    13.             {
    14.      Vector3 newPosition = transform.position;
    15.                newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime;
    16.      transform.position = newPosition;
    17.             }
    18.     }
    19. }
    I fixed it already, but please fix it in the documentation too.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Animator))]
    5.  
    6. public class RootMotionScript : MonoBehaviour
    7. {
    8.     private Animator _animator;
    9.     private Vector3 _newPosition;
    10.  
    11.     private void Start()
    12.     {
    13.         _animator = GetComponent<Animator>();
    14.         _newPosition = transform.position;
    15.     }
    16.  
    17.     void OnAnimatorMove()
    18.     {
    19.         if (!_animator) return;
    20.  
    21.         _newPosition.z += _animator.GetFloat("Runspeed") * Time.deltaTime;
    22.         transform.position = _newPosition;
    23.     }
    24. }
    Sometimes I wonder if the code is even tested before it is released? :confused:
     
  2. vladk

    vladk

    Joined:
    Jul 10, 2008
    Posts:
    167
    haha, nice "fix" :D
     
  3. MagicStyle

    MagicStyle

    Joined:
    Oct 17, 2017
    Posts:
    5
    Is there something wrong with it? If so can you please point it out and improve it please?
     
    Last edited: Nov 24, 2017