#2d acceleration and deceleration
1 messages · Page 1 of 1 (latest)
👋
you mean this one?
Yes
Yes, yes
yes
the problem is now that when I change direction the "currentSpeedX" stays on 500 .. so no acceleration when changing direction
I see
Shouldn't currentSpeedX be clamped between -speedMax and speedMax?
Instead of 0 and speedMax
ok.. I 'll try
And maybe you should check the velocity on the Rigidbody2D as you test, not only on the script
You are actually using the input to determine the direction so this might not be the solution
yeah the player moves to the opposite direction from key press now and keeps moving
no way to stop it
By the way, I think decceleration should be higher than acceleration. Can you try this?
ok I set acceleration to 1500 and deceleration to 2000 but still the same problem ..
should I not use the input to determine the direction?
in another way?
The changeSpeed() part doesn't convice me🤔
Because if the player is going left at 200 units/s and then the next frame turns right, the speed will increase and the player will change direction right away, instead of deccelerating and then accelerating to the other direction.
Decceleration shouldn't be applied only when the player releases the buttons but also when changing directions (these are 2 different types of decceleration)
oof do you know how to apply deceleration when changing directions.. I can't find any helpful tutorials online /:
So, in changeSpeed() you should:
if(turtle.speed > 0){
if(moveDirection.x > 0){
currentSpeed += acc * Time.deltaTime;
}else{
currentSpeed -= dec * Time.deltaTime;
}
//Then the opposite if turtle.speed < 0
It's a bit annoying with lots of nested if-s but this approach has worked for me
ew the turtle.speed gives n error do I have to set sth up for that?
Sorry, turtle.speed.x
mmh it says the rigidbody doesn't have a definition for speed 😱
everthing fine (;
😅
ok just to go sure again
I will remove that and put the other code in?
Remove just the upper half, for velocity x. Then you'll have to do the same for velocity y but finish this first
Add even the rest. So below what you added, add:
...
else{
if(moveDirection.x < 0){
currentSpeedX -= acc * Time.deltaTime;}
else{
currentSpeedX += dex * Time.deltaTime;
}
}
Just remove movedirectionforcalculatingX in the multiplication
Aaand, you shouldn't have removed the clamping
owww it's working to 99% now 😃
but the turtle is always moving a bit
is it possible to stop that?
lol now both work
So is it working?
Yes only the player always moves slightly
Can you show a screenshot of the code with the line numbers? I want to show you how to add the 2nd type of decceleration
like this?
Below line 69 add:
...
else if(moveDirection.x == 0){
currentSpeed -= acc *time;
}
...
and below line 80 add:
...
else if(moveDirection.x == 0){
currentSpeed += acc *time;
}
...
To sum up the code so that you understand what is happening:
if the object is moving right:
- if the player keeps pressing right, add speed by acceleration;
- if the player doesn't press, slow down little by little (in this case by acceleration but if you want you can have another variable);
- if the player is pressing left, slow down by decceleration.
The opposite happens when the player is moving left.
everything sounds very logical and also works
but I do not know why but the player still moves and always in the direction in which I last moved
Maybe due to the huge values of acceleration and decceleration?
Did you add this ^ ?
That happened to me as well, but e-07 is super small so no big deal
Can you show a video of the turtle moving?
And also, if you want the same movement for y, the code should be the same.
just realised that the turtle is jiggling
when I use small numbers for acceleration and decceleration it's fixed but bc of they're so small it takes forever until the turtle is moving
I think decceleration should be higher than acceleration. (Doesn't fix the jiggling, it did happen to me at times as well but at other times it didn't. I think it happens when the player reaches max speed. Try also checking the velocity value in Rigidbody2D)
A velocity value in this rigidbody2d?
Yes, under Info I think
can't change anything there.. but should be on 0 anyway right?
well for now it's working I will leave it as it is rn
THANK YOU FOR YOUR TIME AND HELP 🤩
I mean that you can check as it jiggles what is actually happening to the velocity to find out why it might be happening.