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
1
Question by Hamesh81 · Oct 13, 2012 at 06:02 AM · variabletimefloatanimate

How can I change a float variable over time?

I have a simple script which changes a float variable in another script (via GetComponent) when an input is pressed. At the moment the variable changes instantly but I would like to be able to control the speed at which this float variable changes while the input is pressed. For eg. if I press the input for a short time the float starts to approach the value but only reaches it if the input is pressed for a long enough time. How can I do this? In the "if" statement if I change the "=" to a "-=" of "+=" it does change the float over time but I still can't control how fast it changes.

 using UnityEngine;
 using System.Collections;
 
 public class HydraulicSuspension : MonoBehaviour {
     
     public float lowRider = 0.1f;
     public CarDynamics carDynamics;
     
     //Use this before intialization
     void Awake(){
         carDynamics = GetComponent <CarDynamics>();                
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         
         //Check if Hydraulics is pressed
         if(Input.GetAxisRaw("Hydraulics") == 1) {
             carDynamics.suspensionTravelFront = lowRider;
             carDynamics.SetCarParams();
         }
     }
 }
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

3 Replies

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

Answer by Hamesh81 · Oct 22, 2012 at 09:04 AM

The first answer of this question solved everything for me. For anyone having trouble understanding how to set up a Mathf.lerp, I recommend you read this. It isn't as straight forward as one would think and it took me a while to get it. It is basically what whydoidoit suggested previously but it goes into a few simple examples as well. Hope that helps.

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 gribbly · Oct 13, 2012 at 06:15 AM

Take a look here:

http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html

You'll probably end up with something like:

 function FixedUpdate () {
     if(Input.GetAxisRaw("Hydraulics") == 1) {
         carDynamics.suspensionTravelFront = Mathf.Lerp(min, max, Time.time);
         carDynamics.SetCarParams();
     }
 }

...where min and max are float vars that you've set at the min and max range you want.

To control speed, you can do:

 carDynamics.suspensionTravelFront = Mathf.Lerp(min, max, Time.time * speed);

...where speed is another float var.

Comment
Add comment · Show 8 · 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 Hamesh81 · Oct 13, 2012 at 09:06 AM 0
Share

Perfect! That's exactly what I was looking for. Thanks a lot :)

avatar image whydoidoit · Oct 13, 2012 at 09:12 AM 1
Share

That should be Time.deltaTime added to a float initialised to 0 - not Time.time (which is the number of seconds elapsed since the game started playing and will rapidly be greater than 1 - meaning the Lerp has no effect).

avatar image Hamesh81 · Oct 15, 2012 at 07:22 AM 0
Share

Thanks a lot for your help $$anonymous$$ike. I'm finding it difficult to slow Time.deltatime when multiplying it by the speed variable. I've tried using a float value as small as 0.001 for the speed but it doesn't seem to be any slower that using a speed of 1.0. Any reasons for this?

avatar image whydoidoit · Oct 15, 2012 at 07:26 AM 0
Share

Can you post your code (as a comment is fine)?

avatar image Hamesh81 · Oct 15, 2012 at 07:57 AM 0
Share

Here it is:

using UnityEngine; using System.Collections;

public class HydraulicSuspension : $$anonymous$$onoBehaviour { public float offRoadRider = 0.3f; public float lowRider = 0.1f; public float speed = 0.001f; public bool reachedTop = false; public bool reachedBottom = false; public CarDynamics carDynamics; //Use this before intialization void Awake(){ carDynamics = GetComponent (); } // Update is called once per frame void FixedUpdate () { //Check if HydraulicsUp is pressed if(Input.GetAxisRaw("HydraulicsUp") == 1 && !reachedTop && !reachedBottom) { carDynamics.suspensionTravelFront = $$anonymous$$athf.Lerp(lowRider, offRoadRider, Time.deltaTime speed); carDynamics.suspensionTravelRear = $$anonymous$$athf.Lerp(lowRider, offRoadRider, Time.deltaTime speed); carDynamics.SetCarParams(); Debug.Log ("Up Pressed"); } }

Show more comments
avatar image
0

Answer by MountDoomTeam · Oct 13, 2012 at 11:06 AM

This is a useful script for controlling fire rate:

http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html

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

12 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

Related Questions

How Reset Variable After Loop 1 Answer

Start timer with trigger 1 Answer

Random time activate/deactivate object 1 Answer

Problem with time system and variables 1 Answer

OpenFeint float scores 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