Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 pr6034_unity · Oct 07, 2021 at 08:45 AM · inputfloatdelayinvokehorizontal movement

Access to float seconds ago

So What I want to do is to make my character move 1.0f after I press the arrow key / stop 1.0f after I release the arrow key. To make a delay in the action. So I did some expriments with Invoke here


 private float WalkDelay;
 private float WalkDelay2;
 
 void FixedUpdate()
 {
     WalkDelay = Input.GetAxisRaw("Horizontal");
     Invoke("WDelay", 1.0f);
 
     rb.velocity = new Vector2(WalkDelay2 * speed, rb.velocity.y);
 }
 
 void WDelay()
 {
     WalkDelay = WalkDelay2;
 }



But obviously it won't work cause no matter how much frame I put before the Invoke, the WalkDelay2 will always get the current WalkDelay not WalkDelay Seconds ago. I Know why it doesn't work but I have no idea how i can make it work. Can someone please help me?

+Sorry my code looks all messy in the posting I don't know why it looks like that..

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 Captain_Pineapple · Oct 07, 2021 at 08:47 AM 0
Share

there is a little button over the text entry field with some numbers on it. This is the button you have to click where you can enter code so that the forum adds proper formatting to it.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Oct 07, 2021 at 08:55 AM

Well yes this solution as you currently have it can not work. i'd suggest you try something like this: Something like this following should work as it uses coroutines where you can pass an argument to the function:

 private float lastInput = 0f;
 private float delayedMovementInput = 0f;
 
 public void Update()
 {
     //make sure you always check for input in Update and not in FixedUpdate
     float newInput = Input.getAxisRaw("...");
     if(newInput != lastInput) //whenever the input changes start a coroutine to apply that input delayed
     {
         StartCoroutine(setNewMovementInputDelayed(newInput));
         lastInput = newInput;
     }
 
     rb.velocity = new Vector2(delayedMovementInput * speed, rb.velocity.y); // move by delayedInput
 }
 
 private IEnumerator setNewMovementInputDelayed(float newInput, float delay= 1f)
 {
     yield return new WaitForSeconds(delay);
     delayedMovementInput = newInput;
 }

Let me know if that helped.

Comment
Add comment · Show 1 · 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
avatar image pr6034_unity · Oct 07, 2021 at 03:05 PM 0
Share

OMG thank you sooo much it works!!!!! You're my angel. May God bless you:)

avatar image
0

Answer by Bunny83 · Oct 07, 2021 at 02:07 PM

You could use coroutines like Captain showed in his answer, though this creates garbage. An alternative would be to simply use a Queue. A simple implementation may just delay for a certain amount of frames by simply pre-filling the queue with that amount of values and each frame simply queue the current input and dequeue the oldest. That way the length of the queue stays the same and you get X amount of frames delay. Of course, this is not frame rate independent. Though the length of the queue could be set dynamically once so the only effect keft would be frame rate fluctuations but huge framerate differences between several devices would not be an issue.


Of course you can do also use a queue where you queue up the current input value with a timestamp and when dequeuing the input for this frame, you would simply peek into the queue to see if the oldest time has expired and then dequeue until you get a non-expired time. This would make it fully framerate independent.


Currently I don't have time to write up an example, but you should get the idea ^^.

Comment
Add comment · Show 3 · 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
avatar image pr6034_unity · Oct 07, 2021 at 03:09 PM 0
Share

Thanks for your help friend but since I'm super newbie in coding so I can't really get the idea but I managed to solved the problem somehow with the Captain's code so when I get to be good at coding oneday, i'll try your method:) I do wanna try to get rid of those garbage! even though I'm don't know what exactly they are haha

avatar image Captain_Pineapple pr6034_unity · Oct 07, 2021 at 04:18 PM 0
Share

in some instances when you create objects (in code with new someClass()) this allocates memory. When you don't need the resulting object anymore it is not directly deleted but remains in the memory as so called garbage. Every now and then the garbage collector goes through your memory and cleans up - it removes all old objects which are not referenced anymore. This can lead to lag frames as cleaning the garbage is not really efficient. -> garbage should be avoided as much as possible. Bunny is right here. In the end my solution might be the more straight forward simpler solution but it is not really that efficient memory-wise. Just keep that in $$anonymous$$d if you should ever go for larger projects.

avatar image pr6034_unity Captain_Pineapple · Oct 08, 2021 at 11:26 AM 0
Share

Oh damn yeah garbage seems like a big deal i'll try to find a way to get rid of those when my project get bigger thx :)

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.

Follow this Question

Answers Answers and Comments

162 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.GetAxis returns a value, whilst CrosPlatformInputManager.GetAxis dosn't. 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

2 frame delay from button press till GetKey registers it 1 Answer

Repeating after variable delay 1 Answer

strange behaviour for coroutine and invoke 1 Answer


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