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
0
Question by DarkKnight6452 · Jul 28, 2018 at 02:58 AM · vector3platformer

Fading between two numbers at a speed I choose

Basically I just need the input to slowly go from whatever it was to what it is now. So that if I walk on ice and stop it isnt abrupt, the vector3 will slowly come to a stop...

Not sure how to do it though.

Like if I go forward and the input direction is 1 on the x axis, then I stop and it goes to 0, can I make it fade like 1, 0.9, 0.8, 0.7, 0.6 and so on at a speed of my choosing.

I can use physics materials but I dont want to because I will need this to be in affect when I am in the air too at some points.

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

Answer by Burnoisette · Jul 28, 2018 at 03:14 AM

Try tweening that value with a library like DOTween: http://dotween.demigiant.com

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 UnityNoob123 · Jul 28, 2018 at 03:45 AM

I would just use mathf.Lerp(), or vector3.Lerp. But in this case since it is only 1 axis I would use mathf.Lerp() and apply that function to the x axis of your vector3. Hope this is what you were looking for.

https://docs.unity3d.com/ScriptReference/Mathf.Lerp.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
avatar image
0

Answer by JVene · Jul 28, 2018 at 05:23 AM

I don't disagree with the two answers you have thus far, but since you mention vector3, I thought I'd chime in with a method to do this that incorporates time, because it is the "speed I choose" that brings time into the idea.

Even though you mention a Vector3 on an axis, which can be done on a simple scalar as you example, you can transition between any two Vector3's using the Vector3 class's own lerp (or slerp). No matter how you choose to do this, most interpolation designs (which is really what you're asking to do) use the notion of transitioning between 0 and 1, which is like a fractional expression of percentage where 1.0 equates to 100% of the transition. Lerp differs from Slerp, and you can experiment to determine which you prefer. Eventually you may need to apply this notion to rotation and/or transition, but I've encountered it in a wide range of scenarios you'd not expect, including engineering projects involving steering a robot or controlling a robotic arm (completely outside the domain of Unity).

First, you must have two vectors representing the start and end points. Fashion those.

Next, you need a speed, but that may be interpreted from an English description as being either A) the amount of time it takes to get from point A to point B, or B) an actual rate expressed in meters per second. If you use B, you must express the rate in meters per second because Unity's default unit is the meter.

Method A is a bit easier, because you're not expressing a rate, you're expressing a time to get from A to B. I'll assume:

 float duration = 5.5f;

I use 5.5 as an example of seconds, but you choose any duration you like. In Update (or, preferably a function that update calls to do this), do something like:

 // 'elapsed' is a member of the class, 
 // which much be initialized to zero every time 
 // this sequence begins

 float elapsed = 0f; 

 //  the start and end positions as Vector3

 Vector3 startPos; // don't forget to initialize to new Vector3's
 Vector3 endPos;

While the code above is in the class, declaring variables, what follows are code snippets to be included in the Update function or a function called by Update to process the increments at each frame

 Vector3 position_at_this_time; // to be calculated, the result
 elapsed += Time.deltaTime;
 float interpolate_factor = elapsed / duration; // duration must not be zero

 if ( interpolate_factor <= 1.0f ) 
   { // if interpolate_factor > 1.0f, the endpoint was reached
     position_at_this_time = Vector3.slerp( startPos, endPos, interpolate_factor );
   }

"position_at_this_time" is what you're looking for, if an only if you're more concerned about the duration and not the speed.

"interpolate_factor" is that value between 0 and 1 most of the lerp, slerp or other similar "between" functions need. When it reaches 1.0f, the transition is complete. This example is oversimplified, you may need to clamp to 1.0f, and change a "bool" or something related to indicate the transition is over

However, if speed is the primary driving parameter you want, fashion that as meters per second and do something like:

 // 'elapsed' is a member of the class, 
 // which much be initialized to zero every time 
 // this sequence begins
 
 float elapsed = 0f; 

 //  the start and end positions as Vector3

 Vector3 startPos; // don't forget to initialize to new Vector3's
 Vector3 endPos;

 float speed = 2.5f; // 2.5 meters per second
 float duration; // this will be calculated in this version, based on speed

While the code above is in the class, declaring variables, the following is a code snippet that must appear in a function that initializes the motion

    // this code must be called to initiate the motion, setting the
    // values to operate at the given speed

    float distance = Vector3.Distance( VectorA, VectorB );
    duration = distance / speed; // speed must not be zero  
     

This merely calculates the duration the transition will require at this speed. This makes it the same as the previous method which merely took a duration of user choice (which is why I mentioned both). The code calculating the "position_at_this_time" is, therefore, identical.

This must be wrapped in something which initiates the motion, and terminates when the interpolate_factor reaches 1.0f. You may want to take the extra step of "planting" the last position. That is, the next to last frame might be at, say, 0.99f, which is very close but not quite 1.0. The next frame that comes up might be, say, 1.0001f, which triggers the end of the transition. It is, however, beyond the transition, to the value passed to a lerp or slerp must be clamped to 1.0f, planting the last entry of this series before fully terminating the motion.

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

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

Related Questions

Vector 3 help? 0 Answers

2D 360 degress platformer example needed 0 Answers

Vector 3.up error 1 Answer

Incorrect velocity after collision 1 Answer

Creating the After Effects Wiggle expression to effect rotation in C# 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