#So what you can do is read `keys` into a

1 messages · Page 1 of 1 (latest)

analog eagle
#

I don't think I understand what you meant here. Here look at what I have tried to do:

        startTimer();
        slideCurve.MoveKey(0,); //I am accessing the first the first index, and the second argument is a KeyFrame, which idk what it is
        speedDuringSliding = slideCurve.Evaluate(time);
        rb.velocityX = speedDuringSliding * direction;
    }```
If you are wondering what I am trying to do here, I am trying to move the first key towards the current velocity of the player and the player speed will decelerate by using the AnimationCurve.
errant zealot
#

i don't understand why you need to modify the animation curve here

analog eagle
#

so depending on where the current velocity is at, if the player slides, then the player will slide starting at that player velocity

errant zealot
#

that doesn't make any sense

#

you start sliding at your current velocity

#

don't you already know that?

analog eagle
errant zealot
#

why is an animation curve needed at all here?

#

describe the gameplay you're trying to create here

analog eagle
#

alright, so when the player moves, there will be an acceleration when running, which is accomplished by using an animation curve. Now if the player decides to slide before reaching its max speed, I want the player to slide starting at the current velocity and then it will slowly go down to zero depending on how the curve is structured. This is why I need to modify the the very first point in the graph.

errant zealot
#

but the curve's only job is to tell you your current acceleration

#

or does the curve tell you the velocity you can move at?

#

show me your entire movement script

analog eagle
#

oh it is really long code. I have only showed you the fundemental stuff

analog eagle
errant zealot
#

Share the entire thing. I can read it.

analog eagle
errant zealot
#

sounds like you should just have time go down towards zero during a slide

analog eagle
#

huh, what do you mean?

errant zealot
#

you add Time.deltaTime to the time field every time you call run()

#

why not just do the opposite in slide()?

analog eagle
#

ohhhh that is actually really smart

errant zealot
#

startTimer is a bad name for that function

#

it doesn't start a timer at all

#

time is also a very vague field name

#

I'd call it accelerationProgress or something like that

analog eagle
errant zealot
#

maybe SpeedUp

#

and then SlowDown

analog eagle
#

or that

errant zealot
#

you should also give it an argument to control how quickly you speed up or slow down

#
public void SpeedUp(float rate = 1) {
  accelerationProgress = Mathf.MoveTowards(accelerationProgress, 1, Time.deltaTime * rate);
}
#

using MoveTowards stops it from going past 1

#

and then rate lets you control how quickly it changes

#

SlowDown would move towards 0 instead of 1

analog eagle
#

ohh I have never seen that Mathf.MoveTowards function

analog eagle
#

wait I just realized a flaw here, it still wouldn't adjust where the current velocity would be. We are just doing the whole thing in reverse when we could have just done it normaly.

errant zealot
#

I don't know what "normally" means

#

rewriting your acceleration curve does not sound very normal to me

#

I also don't know what you mean by "Adjust where the current velocity would be"

#

as you run, your acceleration progress moves towards 1

#

if you slide, the progress moves towards 0 instead

#

at the moment that you start sliding, your velocity will start going down instead of up

#

i should point out that MoveKey is changing the animation curve. You'd be permanently changing the shape of the curve.

analog eagle
# errant zealot at the moment that you start sliding, your velocity will start going down instea...

yea but what if you slide in the middle of your acceleration. Lets say your current velocity is 3, and you start accelerating. In the middle of your acceleration, your current velocity would be at 5 and the max will be 7. Now, if you start sliding, you don't start at a velocity of 5, because remember, it all depends on where the key is placed in the graph. So it can make it start at the walking speed of 3, or start at the max speed of 7. This will be very weird because there is a sudden change in velocity which is why I want to set the keyframe at the current velocity.

errant zealot
#

you accelerate up to 5

#

your acceleration progress is now at 5/7

#

you start sliding. your acceleration progress is still at 5/7, so your speed doesn't change

#

then you acceleration progress goes backwards, so your velocity goes down

#

I do not see the problem.

analog eagle
#

ok I think it would be better if I draw it out for you.

#

you see how it goes from a velocity of 5 when accelerating to a velocity of 7 when initiating sliding

#

the speed diffrence will be noticable

#

which is why I need the program to automatically set the initial point to where the current velocity will be at

errant zealot
#

so sliding instantly speeds you up to your maximum velocity?

analog eagle
#

yea if I set the the keyframe at that point

errant zealot
#

no keyframes

#

i am asking you how your game is going to work

#

you're in an XY problem right now

#

Y is "how do I set a keyframe on an animation curve?"

errant zealot
analog eagle
#

no, it starts at where the current velocity is at

errant zealot
#

okay, so if you're at 75% of your maximum speed, and you start sliding, you should be sliding at 75% of your maximum speed

#

and then slow down until you hit 0%

#

is that correct?

analog eagle
#

yes exactly

errant zealot
#

when you start sliding, the red dot will begin moving to the left

#

or do you have a separate animation curve you want to use for slowing down?

analog eagle
#

I am using a seperate animation curve

errant zealot
#

okay, that's the important bit

#

I would suggest just making that curve range from 0 to 1

#

Store your velocity into a variable when you begin a slide

#

your velocity is now decelerationCurve.Evaluate(time) * slideSpeed

#

slideSpeed is set when the silde begins

#

You could do the same thing for the acceleration curve: make it range from 0 to 1, and store the max speed separately

analog eagle
errant zealot
#

we don't have to mess with the curve

#

the curve is now "normalized" -- it ranges from 0 to 1, no matter how fast you're actually sliding

#

it tells you what fraction of your original speed you still have

#

when you multiply that by slideSpeed, you get your current speed

#

if the curve is at 0.3, you're moving at 30% of your original sliding speed

analog eagle
#

ohhh ok yea that makes sense

errant zealot
#

you're separating the configuration (the curve's shape) from the runtime data (the speed you had when you started sliding)

#

mixing the two gives you a headache

analog eagle
#

Hey Fen, I know this discusion happened a long time ago, but I am having a little bit of an issue here.

#

When applying what you have told me to my running function (aka acceleration), it works perfectly

#

however, when applying it to my sliding function, it is giving me a janki result

#

here is my code:

        SpeedDown(slideRate);
        speedDuringSliding = slideCurve.Evaluate(deccelerationProgress) * initialHorizontalSpeed; //Multiply the initial horizontal speed to start sliding at the current velocity 
        rb.velocityX = speedDuringSliding * direction;

    private void SpeedDown(float rate = 1) {
        deccelerationProgress = Mathf.MoveTowards(deccelerationProgress, 0, Time.deltaTime * rate);
    }
    ```
#

I have debugged the deccelerationProgress value and the x velocity, and the result was that as the deccelerationProgress drops down, the x velocity becomes 0 before deccelerationProgress even reaches 0

errant zealot
#

you shouldn't be changing speedDuringSliding constantly

#

you should set it once when you start to slide

analog eagle
errant zealot
#

No. speedDuringSliding is how fast you were going when you started to slide.

#

Oh, I misunderstood

#

You have a separate initialHorizontalSpeed

#

Perhaps you're changing initialHorizontalSpeed somewhere else?

analog eagle
#

ill check

#

yes, you were right. I have this function right here:

    private void recordHorizontalMovementSpeed() {
        if (Input.GetKey(KeyCode.A) == true) {
            initialHorizontalSpeed = -rb.velocity.x;
        }
        else if (Input.GetKey(KeyCode.D) == true) {
            initialHorizontalSpeed = rb.velocity.x;
        }
    }
#

how do I not make a variable change after saving it? I know that I have to save it to another temporary variable, but that variable will also change with it.

errant zealot
#

don't record the current speed if you're not running

analog eagle
#

oh yea that makes sense

#

ok now it works as intended 🙂

#

one last question

#

how do I make the sliding stop at a velocity of 4. (meaning once the velocity reaches 4, then the velocity will be equal to 0)

#

is there an easy way I can I can do this in the calculation without using any if statment?

errant zealot
#

i don't see why an if statement is a problem

#

and given that this is literally an "if X, then Y" situation, an if statement sounds appropriate to me