- Home /
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..
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.
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.
OMG thank you sooo much it works!!!!! You're my angel. May God bless you:)
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 ^^.
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
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.
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
Follow this Question
Related Questions
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Input.GetAxis returns a value, whilst CrosPlatformInputManager.GetAxis dosn't. 1 Answer
2 frame delay from button press till GetKey registers it 1 Answer
Change a float smooth 2 Answers
Textfield number only 1 Answer