c# - Player movement starts after a few seconds after pressing the button -


i'm following tutorial on unity 5 create simple stealth game.

i followed instructions create script controls movement of player. when tested game noticed player takes few seconds after pressing button before moving.

it's if before moving should await conclusion of rotation performed quaternion.lerp.

also pressing x button should scream attract attention , take proper animation.. runs sound the animation not done.. performed once in multiple tests did.

public class playermovement : monobehaviour  {     public audioclip shoutingclip;      // audio clip of player shouting.     public float turnsmoothing = 15f;   // smoothing value turning player.     public float speeddamptime = 0.1f;  // damping speed parameter       private animator anim;              // reference animator component.     private hashids hash;               // reference hashids.       void awake ()     {         // setting references.         anim = getcomponent<animator>();         hash = gameobject.findgameobjectwithtag(tags.gamecontroller).getcomponent<hashids>();          // set weight of shouting layer 1.         anim.setlayerweight(1, 1f);     }       void fixedupdate ()     {         // cache inputs.         float h = input.getaxis("horizontal");         float v = input.getaxis("vertical");         bool sneak = input.getbutton("sneak");          movementmanagement(h, v, sneak);     }       void update ()     {         // cache attention attracting input.         bool shout = input.getbuttondown("attract");          // set animator shouting parameter.         anim.setbool(hash.shoutingbool, shout);          audiomanagement(shout);     }       void movementmanagement (float horizontal, float vertical, bool sneaking)     {         // set sneaking parameter sneak input.         anim.setbool(hash.sneakingbool, sneaking);          // if there axis input...         if(horizontal != 0f || vertical != 0f)         {             // ... set players rotation , set speed parameter 5.5f.             rotating(horizontal, vertical);             anim.setfloat(hash.speedfloat, 5.5f, speeddamptime, time.deltatime);         }         else             // otherwise set speed parameter 0.             anim.setfloat(hash.speedfloat, 0);     }       void rotating (float horizontal, float vertical)     {         // create new vector of horizontal , vertical inputs.         vector3 targetdirection = new vector3(horizontal, 0f, vertical);          // create rotation based on new vector assuming global y axis.         quaternion targetrotation = quaternion.lookrotation(targetdirection, vector3.up);          // create rotation increment closer target rotation player's rotation.         quaternion newrotation = quaternion.lerp(getcomponent<rigidbody>().rotation, targetrotation, turnsmoothing * time.deltatime);          // change players rotation new rotation.         getcomponent<rigidbody>().moverotation(newrotation);     }       void audiomanagement (bool shout)     {         // if player in run state...         if(anim.getcurrentanimatorstateinfo(0).namehash == hash.locomotionstate)         {             // ... , if footsteps not playing...             if(!getcomponent<audiosource>().isplaying)                 // ... play them.                 getcomponent<audiosource>().play();         }         else             // otherwise stop footsteps.             getcomponent<audiosource>().stop();          // if shout input has been pressed...         if(shout)             // ... play shouting clip are.             audiosource.playclipatpoint(shoutingclip, transform.position);     } } 

i'm new in unity might need more explanation. everyone!

the first issue, delaying player movement, may caused line

anim.setfloat(hash.speedfloat, 5.5f, speeddamptime, time.deltatime); 

the potential issue calling time.deltatime, intended use inside update() call. function called inside of fixedupdate() means should using time.fixeddeltatime.

for second issue, shouting, code seems fine , issue in animation tree. check state in before shouting can transition shout, , checks correct trigger.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -