- Home /
How to check a value from the last frame, while using events.
What I am trying to do is check if a value has changed from the last frame, while using events. Typically I could use threading to my advantage and have a check at the bottom of Update() to see if a value that is typically changed higher up in Update() was changed or not.
However, I am programming with Events, and I don't rely on Update() in several classes to change data. When an event is published, it makes method calls to all methods subscribing to that event. CameraUpButton() is called by the event manager, not by Update(). ClearUp() is called from Update().
I want to see how long an event has been constantly published for, essentially seeing how long the up button has been held. Checking how long it has been held is the easy part, clearing it once the button is no longer held is the hard part since I do not know how to see when an event stopped publishing (I don't know if that is even possible?). So I'm resorting to making some logic to figure it out for me.
In this case:
private int up;
private void CameraUpButton()
{
up++;
Vector3 cameraPos = Camera.main.transform.position;
cameraPos.y += 0.01f;
Camera.main.transform.position = cameraPos;
}
private void ClearUp()
{
some logic to determine
if up has changed since the last frame
}
Edit: I can make a Input.GetButtonUp("") event, however I want to see if this can be solved without relying on an event triggered by the button coming up. I would prefer if it can be solved without that reliance.
Answer by douglasg14b · Jan 25, 2015 at 06:47 AM
So I managed to figure this out.
To check a value from the last frame I ended up doing a few things:
public bool eventFiredLeft;
private void EdgeOfScreenLeft()
{
if (userInput.MousePos.x <= ((0 + (userInput.screenRect.width - (userInput.screenRect.width * userInput.edgeOfScreenSize)))) && EdgeOfScreenEvent != null)
{
if(userInput.MousePos.y < userInput.screenRect.height * userInput.cornerOfScreenSize &&
userInput.MousePos.y > userInput.screenRect.height -(userInput.screenRect.height * userInput.cornerOfScreenSize) &&
userInput.isMouseOnScreen)
{
EdgeOfScreenEvent("left");
eventFiredLeft = true;
}
}
else if (eventFiredLeft == true && EdgeOfScreenEventEnded != null)
{
EdgeOfScreenEventEnded("left");
eventFiredLeft = false;
}
}
First you see a bool that I have to declare somewhere outside of the method, so I don't have to initialize it with each loop. I then check to see if the mouse is withing a specific area of the screen where the event will be triggered as seen with the first if statement (the next if statement in the chain is to narrow that area down a bit).
If the mouse was in that area which means the event was fired, the bool that was declared earlier is set to true. Upon the next frame if the mouse is no longer in that area the first if statement is false and it moves on to the next else if. This checks if the bool is already true, which indicated that the event was fired during the last frame. It will then fire an event saying that the previous event has ended and set the bool back to false.
I use this to signal the end of the event so that I can reset a timer I have linked to modify the cameras movement speed based on the amount of time the mouse was in that position.
If anyone else knows of a better method, let me know.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180740im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get Sprite Animation to play EVERY frame 0 Answers
Unity 5.5 Playing inside an animation forward and backwards. 0 Answers
Basic beginning javascript question. (logging position and moving character to logged position) 2 Answers
Do "Dynamic Public Strings" effect iPhone Performance? 1 Answer
Map Panning 2 Answers