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 Oninji · Oct 12, 2010 at 10:13 PM · javascriptbuttonvariablemath

On Button press Move Value toward

Hellow,

In short, I want to move a value toward another one WHILE the associated button is pressed.

At the moment, i've tried Mathf.Lerp and Mathf.MoveTowards but no success.

This is the assiociated part of the code.

var power = 0; var speed = 5; var powermax = 5000; var powermin = 100;

 if(Input.GetButton("Pin")) {
      power = Mathf.MoveTowards(powermin, powermax, speed * Time.deltaTime);

 }

At the moment, all it does is after 1-2 second, the power switch from 0 to 110 and stay there.

PS: To be clear, there is no physics involved in this snippet of code. All I'm trying to do is to move a value A toward a value B within an incrementation speed, this only while the associated key is pressed.

Example:

Charging a Spell Power: Initial power = 0 Final power = 1000 Increase power by 10 per second until it reach Final power.

Comment
Add comment · Show 1
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 · Oct 12, 2010 at 11:01 PM 0
Share

From your question, I wasn't sure if you wanted it to reset when you let go of "Pin" - if that's the case, comment on my answer and I'll update the code.

2 Replies

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

Answer by · Oct 12, 2010 at 11:00 PM

This will change the power value linearly from min to max. It uses Mathf.Lerp, and the amount that the lerpAmt changes by is affected by the timeToMove.

var timeToMove : float = 5.0; // time to move (in seconds) private var lerpAmt : float = 0.0; // current lerp 't' amount (private, 0-1)

if ( Input.GetButton("Pin") // if pin is pressed && lerpAmt < 1 ) // and lerpAmt is not already at max { lerpAmt += Time.deltaTime / timeToMove; power = Mathf.Lerp(powermin, powermax, lerpAmt); }

The code is untested but should work. Let me know if you have any troubles.

Comment
Add comment · Show 1 · 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 Oninji · Oct 12, 2010 at 11:12 PM 0
Share

Thanks, it seems to work.

avatar image
0

Answer by AVividLight · Oct 12, 2010 at 10:21 PM

Hey Oninji,

If I understand your question... It seams to be a reoccurring topic on the forums... See, you can't add force, speed, etc. If you aren't already moving...

Example:

If your standing still, and think "I want to run", you can't run unless you start moving...

If I were you I would give your player movement first, then give it a "speed boost"...

-Gibson

Comment
Add comment · Show 1 · 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 Oninji · Oct 12, 2010 at 10:27 PM 0
Share

No, I simply want to move a value.

The rest of the script already work with a set Value. All I need to do is to make the "Power Value" move towards his "$$anonymous$$ax power". A bit like, charging an attack.

There is no real movement happening, it's just a question of moving a numerical value.

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

No one has followed this question yet.

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Maths with variables 2 Answers

Calculations Wrong? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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