• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by raxor95 · Jul 27, 2015 at 09:58 AM · c#animationlerpplayer movementlatency

Player movement starts after a few seconds after pressing the button

I'm following a tutorial on Unity 5 to create a simple stealth game (you can find it here: http://unity3d.com/learn/tutorials/projects/stealth/player-movement?playlist=17168 ).

I followed the instructions to create the script that controls the movement of the player. When I tested the game I noticed that the player takes a few seconds after pressing the button before moving.

It's as if before moving should await the conclusion of the rotation that is performed by Quaternion.Lerp.

Also pressing the x button should scream to attract attention and take proper animation.. It runs the sound but the animation is not done.. Was performed only once in multiple tests I did.

 public class PlayerMovement : MonoBehaviour 
 {
     public AudioClip shoutingClip;      // Audio clip of the player shouting.
     public float turnSmoothing = 15f;   // A smoothing value for turning the player.
     public float speedDampTime = 0.1f;  // The damping for the speed parameter
 
 
     private Animator anim;              // Reference to the animator component.
     private HashIDs hash;               // Reference to the HashIDs.
 
 
     void Awake ()
     {
         // Setting up the references.
         anim = GetComponent<Animator>();
         hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
 
         // Set the weight of the shouting layer to 1.
         anim.SetLayerWeight(1, 1f);
     }
 
 
     void FixedUpdate ()
     {
         // Cache the inputs.
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
         bool sneak = Input.GetButton("Sneak");
 
         MovementManagement(h, v, sneak);
     }
 
 
     void Update ()
     {
         // Cache the attention attracting input.
         bool shout = Input.GetButtonDown("Attract");
 
         // Set the animator shouting parameter.
         anim.SetBool(hash.shoutingBool, shout);
 
         AudioManagement(shout);
     }
 
 
     void MovementManagement (float horizontal, float vertical, bool sneaking)
     {
         // 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);
     }
 
 
     void Rotating (float horizontal, float vertical)
     {
         // Create a new vector of the horizontal and vertical inputs.
         Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
 
         // Create a rotation based on this new vector assuming that up is the global y axis.
         Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
 
         // Create a rotation that is an increment closer to the target rotation from the player's rotation.
         Quaternion newRotation = Quaternion.Lerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
 
         // Change the players rotation to this new rotation.
         GetComponent<Rigidbody>().MoveRotation(newRotation);
     }
 
 
     void AudioManagement (bool shout)
     {
         // If the player is currently in the run state...
         if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
         {
             // ... and if the footsteps are not playing...
             if(!GetComponent<AudioSource>().isPlaying)
                 // ... play them.
                 GetComponent<AudioSource>().Play();
         }
         else
             // Otherwise stop the footsteps.
             GetComponent<AudioSource>().Stop();
 
         // If the shout input has been pressed...
         if(shout)
             // ... play the shouting clip where we are.
             AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
     }
 }

I'm new in unity so I might need some more explanation. Thanks to everyone!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Jul 27, 2015 at 07:30 AM 0
Share

Try using Time.fixedDeltaTime in $$anonymous$$ovement$$anonymous$$anagement ins$$anonymous$$d of Time.deltaTime

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by raxor95 · Jul 27, 2015 at 05:40 PM

Thanks. I solved the problem that was due to problems in the transitions of my Animation Tree. I removed the flag to "Has Exit Time".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to flip my player left and right? 1 Answer

How do I check if a player is holding a trick and not just tapping it (but i want both) 0 Answers

Multiple Cars not working 1 Answer

Gun is not rotating while player is in a animation 0 Answers

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges