This time the code example is used in conjunction with the animation system
4.3 The new animation system allows animations with positional offsets, just tick apply Root motion in the animator component and we can use it.
usingUnityengine;usingSystem.Collections; Public classdoneplayermovement:monobehaviour{ PublicAudioClip Shoutingclip;//Audio clip of the player shouting. Public floatturnsmoothing = 15f;//A Smoothing value for turning the player. Public floatSpeeddamptime =0.1f;//The damping for the speed parameter PrivateAnimator Anim;//Reference to the animator component. PrivateDonehashids Hash;//Reference to the hashids. voidAwake () {//Setting up the references.Anim = getcomponent<animator>(); Hash= Gameobject.findgameobjectwithtag (Donetags.gamecontroller). Getcomponent<donehashids>(); //Set The weight of the shouting layer to 1.Anim. Setlayerweight (1, 1f); } voidfixedupdate () {//Cache the inputs. floatH = Input.getaxis ("Horizontal"); floatv = Input.getaxis ("Vertical"); BOOLSneak = Input.getbutton ("Sneak"); Movementmanagement (H, V, sneak); } voidUpdate () {//Cache the attention attracting input. BOOLShout = Input.getbuttondown ("attract"); //Set the animator shouting parameter.Anim. Setbool (Hash.shoutingbool, shout); Audiomanagement (Shout); } voidMovementmanagement (floatHorizontal,floatVerticalBOOLsneaking) { //Set The sneaking parameter to the sneak input.Anim. Setbool (Hash.sneakingbool, sneaking); //If There is some axis input ... if(Horizontal! = 0f | | Vertical! =0f) {//... set the players rotation and set the speed parameter to 5.5f.rotating (horizontal, vertical); Anim. SetFloat (Hash.speedfloat,5.5f, Speeddamptime, time.deltatime); } Else //Otherwise Set the speed parameter to 0.Anim. SetFloat (Hash.speedfloat,0); } voidRotating (floatHorizontal,floatvertical) { //Create A new vector of the horizontal and vertical inputs.Vector3 targetdirection =NewVector3 (horizontal, 0f, vertical); //Create A rotation based on this new vector assuming, which is the global Y axis.quaternion targetrotation =quaternion.lookrotation (targetdirection, vector3.up); //Create A rotation that was an increment closer to the target rotation from the player ' s rotation.quaternion newrotation = Quaternion.lerp (rigidbody.rotation, targetrotation, turnsmoothing *time.deltatime); //The change of the players rotation to this new rotation.Rigidbody. Moverotation (newrotation); } voidAudiomanagement (BOOLshout) { //If The player is currently in the run state ... if(Anim. Getcurrentanimatorstateinfo (0). Namehash = =hash.locomotionstate) {//... and if the footsteps is not playing ... if(!audio.isplaying)//... play them.Audio. Play (); } Else //Otherwise Stop the footsteps.Audio. Stop (); //If The shout input has been pressed ... if(Shout)//... play the shouting clip where we are.Audiosource.playclipatpoint (Shoutingclip, transform.position); }}