Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rafal Sankowski · Apr 02, 2010 at 12:28 PM · timechangesmoothvaluessmoothly

How to smootlhy change values in amout of time?

I know it can be kinda easy, but...

I'm trying to make my character run if I'll press "Fire1". So the speed of my character should smoothly change into higher value, from 6 to 15.

I tried to do it by Animation Curves, but it seems like i can't apply 2 other animations to 1 object at the same time (like for example run and crouch).

So... is it any other good way, that allowys me to take control above evertything (time, when value changes)? Och, and please comment everything, cause I'm new in programming.

Thanks!

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
2
Best Answer

Answer by dhendrix · Apr 02, 2010 at 02:46 PM

Do you know about Time.deltaTime? (the time that has elapsed since last update)

var speedMin = 6.0; var speedMax = 15.0; var accelerateTime = 3.0; private var currentSpeed = 0.0; private var timePassed = 0.0;

function FixedUpdate() { if (Input.GetAxis("Fire1")) { timePassed += Time.deltaTime; currentSpeed = Mathf.Lerp(speedMin, speedMax, timePassed/accelerateTime);

} }

The public vars can be adjusted in the editor, you can adjust "accelerateTime", and the character should speed up faster.

How you apply this "currentSpeed" value depends on how you are moving your character. If using a characterController, this link may help http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html

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 Rafal Sankowski · Apr 02, 2010 at 03:32 PM 0
Share

Its nice, but it's not that detailed, as i want to. It increase speed. Okay. But I want something, like I choose the time, when the variable change into max, not like I choose how much it increases. In my GDD i have "speed up from 6 to 15 in 3 seconds" and I cannot make it by your way.

avatar image dhendrix · Apr 02, 2010 at 04:14 PM 0
Share

I've edited my answer to reflect what you seem to want. You may want to also use Input.GetButtonUp("Fire1") to reset "timePassed" to 0, when the player stops running.

avatar image
0

Answer by Rafal Sankowski · Apr 02, 2010 at 09:04 PM

That does not work. I have code like that:

if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){ timePassedSpeed += Time.deltaTime; currentSpeed = Mathf.Lerp(speedMin, speedMax, timePassedSpeed/accelerateTime); }

else{ timePassedSpeed += Time.deltaTime; currentSpeed = Mathf.Lerp(speedMax, speedMin, timePassedSpeed/accelerateTime); }

So i simply want to accelerate, when any arrow key is down, and decrease speed, when it's not. Its smooth, when variable "currentSpeed" do not equals speedMin or speedMax. When it equals that and I try to accelearate or decrease speed, variable changes immadietly from 6 to 15, or from 15 to 6.

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 thieum · Sep 08, 2011 at 11:39 AM

Hi, what you need is Mathf.SmoothDamp function. Then it shifts from var A to var B through time. there is a tricky thing with this function using ref yVelocity to (eventually) smooth the curve between A and B...

so first declare : private float (or var) y Velocity = 0; public float transTime(or whatever) = 1f; (fixed time to go from a to b, I guess we can use a function too, but should be sufficient)

then in your Update: if (Input.GetKey (KeyCode.UpArrow)) { currentSpeed = Mathf.SmoothDamp (speedMin, speedMax, ref yVelocity, transTime);

I hope it 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

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

1 Person is following this question.

avatar image

Related Questions

Change position (hover) over time with easing? 1 Answer

Smooth Screen Orientation Change on iOS devices 0 Answers

position change smoothed? 1 Answer

How to find all objects with an tag and change an value in all of them? 1 Answer

how to smoothly rotate an object to inverse direction 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