• 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 saswilson · Aug 13, 2013 at 01:09 PM · mathrelative

Quick math question - how to keep 2 numbers relative

Hi, just a quick math question.. This is related to my other question so hopefully one answer will be good for both. I have one int (coins) that can be collected and 100 is the max. And I have a float (animSpeed) that is set to 1.5f, and can not go below 1.0f.

So how can I link both together, so if 50 coins are collected, the (animSpeed) float is set to 1.25f

The amount of coins will vary, so I need a simple line a math that will check the coin variable and then adjust the animSpeed float. Please help!

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

2 Replies

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

Answer by KiraSensei · Aug 13, 2013 at 01:12 PM

 animSpeed = 1+(coins/200.0);

Or maybe

 animSpeed = 1.5-(coins/200.0);

It depends on what you want, if you want the speed to decrease while your number of coins increase, or the other way around...

Comment
Add comment · Show 6 · 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 saswilson · Aug 13, 2013 at 01:24 PM 0
Share

I want the animSpeed to decrease as the coins increase. But also, when coins are lost, the coins decrease, I want the animSpeed to increase.

avatar image KiraSensei · Aug 13, 2013 at 01:25 PM 0
Share

So try animSpeed = 1.5-(coins/200.0); in the update method, or maybe when you increase the number of coins only, it is better for performances.

avatar image saswilson · Aug 13, 2013 at 01:39 PM 0
Share

Ok thanks, by the way the animSpeed if a float and coins is an int.

Its giving me an error "Cannot implicitly convert type double' to float'. An explicit conversion exists (are you missing a cast?)"

avatar image saswilson · Aug 13, 2013 at 01:41 PM 0
Share

Its ok I have it working thanks again

avatar image saswilson · Aug 13, 2013 at 01:42 PM 0
Share

Just one more thing, if I want to coins to be no more than 100, how can I use that to stop anymore adding?

Show more comments
avatar image
1

Answer by Jamora · Aug 13, 2013 at 01:36 PM

You can also use Mathf.Lerp.

 animSpeed = Mathf.Lerp(minAnimSpeed,maxAnimSpeed,currentCoins/maxCoins);

That will return minAnimSpeed when curentCoins/maxCoins == 0, maxAnimSpeed when the division == 1 and their average when the the division returns 0.5f, etc.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Project a position onto a plane whose normal isn't at the origin. 2 Answers

How to get vector X distance from point on Y angle? 4 Answers

Basic vector math addition 1 Answer

Getting an inversely proportionate value 1 Answer

Convert screenspace coordinates (top-left origin) to worldspace coordinates. Read before you react! 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