Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 douglasg14b · Jan 18, 2015 at 10:02 PM · eventslogicframes

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.

Comment
Add comment
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

1 Reply

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

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.

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


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