- Home /
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.
Answer by Burnoisette · Jul 28, 2018 at 03:14 AM
Try tweening that value with a library like DOTween: http://dotween.demigiant.com
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.
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.
Your answer
Follow this Question
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