Unity3d Move and Rotate 2

Source: Internet
Author: User

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); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.