Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Rhylvin2015 · Aug 11, 2015 at 07:53 AM · variableintlogicdetectincrease

detect if a variable has stop increasing

hello, I have this variable that keeps on increasing as time passes. along the way it will stop increasing. so how do i detect that so i could make an if statement when it happens.

Comment
Add comment · Show 4
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 Rhylvin2015 · Aug 11, 2015 at 08:39 AM 0
Share

um sorry for not being clear guys. my variable is 'distance' and it increases overtime since i have a 2d ifinite runner. it stops sometimes for reasons not important. the reason why i want to detect this is because i want to stop the background animation. cause it looks weird when you the player stop getting more distance, but the background scrolling animation is still going on. I can stop the animation no problem, the problem is just the detection so I can stop it.

avatar image NeverHopeless · Aug 11, 2015 at 09:40 AM 0
Share

@Rhylvin2015, Have you looked at my suggestion ?

avatar image Rhylvin2015 · Aug 11, 2015 at 11:30 PM 0
Share

@NeverHopeless im sorry but "if(Input.GetAxis("Horizontal"))" is confusing me is it being use everytime? or when Input is on horizontal only. i kinda need it to would work everytime.

avatar image NeverHopeless · Aug 12, 2015 at 08:17 AM 0
Share

@Rhylvin2015, if there is an input for this axis it will go inside if statement. I just give an example, not sure how did you setup things in your game for taking input from user.

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by allenallenallen · Aug 11, 2015 at 08:00 AM

How often does this variable increase? If you do know the time interval it increases though, that would be the best.

 public int number = 0;
 public int lastNumber = -1;
     
 if (number == lastNumber){
     // number has stopped increasing.
 }
 else {
     lastNumber = number;
 }
     


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
avatar image
1

Answer by GiyomuGames · Aug 11, 2015 at 08:02 AM

Store its value in a "previousValue" variable and then at each cycle (update) check if value <= previousValue. If "yes" it means it has stopped increasing.

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
avatar image
0

Answer by NeverHopeless · Aug 11, 2015 at 08:31 AM

Possibly you can handle it using a flag variable. The condition opposite to which increases the variable can be used. This way you don't have to compare everytime if the current value is similar to the last one. If the flag variable is set then you are good to go.

For example:

 bool isRunning;
 float  distance;
 
 void Update()
 {
     if(Input.GetAxis("Horizontal"))
     {
          distance += 10.0f;
          isRunning = true;
     }
     else 
     {
          isRunning = false;
 
          // Variable stop increasing
     }
 }
 
 void SomeFunction()
 {
     if(!isRunning)
     {
         // You can go with this
     }
 }





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
avatar image
0

Answer by Eno-Khaon · Aug 12, 2015 at 12:09 AM

So, if I got this right, you're using your variable "distance" as your global score, essentially. When -something- happens (pausing the game could serve as an example), the "distance" variable no longer increases, but the background continues scrolling by.

Why not simply link the background scrolling to your "distance" variable? You could use something along the lines of:

 float screenWidth = 30.0f; // Distance to scroll before repeating or replacing texture

 // ...

 background.position = new Vector2(Mathf.Repeat(distance, screenWidth), 0.0f);

Using something along these lines, your background's position directly correlates with the distance traveled, so if your score stops rising, the background stops moving.

Then, you can have another variable influence the speed further with a multiplier, so rather than just using "distance", it's "distance * scale" instead (with a few tweaks, but still...). That way, the scale can make the scrolling speed up or slow down to fit the mood of the game at the time, or to focus on background elements, but whether it actively moves or not would still be dependent on whether the distance variable is changing.

An alternative to using "distance" directly would be to instead make relative movements based on the amount "distance" changes per frame.

 float distanceDelta;
 
 // ...
 
 distanceDelta = distance - lastFrameDistance;
Comment
Add comment · Show 2 · 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 Rhylvin2015 · Aug 12, 2015 at 04:19 AM 0
Share

how can you get that lastframeDistance?

avatar image Eno-Khaon · Aug 12, 2015 at 06:54 PM 0
Share

Just before you calculate your distance variable, you can define the value of what it was before the current frame.

 // First, set your previous distance to what it was last.
 lastFrameDistance = distance;
 
 // Then, calculate your new distance value.
 distance = (However you're deciding it);

The difference between the two is how far you traveled in one frame.

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

28 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

Related Questions

controlling the increment in the variable 1 Answer

Pass a function(float) as variable 2 Answers

Displaying variable on UI text every frame (JS) 1 Answer

How do Generate Random Unique int ? 1 Answer

How to write "if (var int = var int 2)" ? 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