#2d acceleration and deceleration

1 messages · Page 1 of 1 (latest)

jolly crown
#

thread create

muted shard
#

👋

jolly crown
#

you mean this one?

muted shard
#

Yes

jolly crown
#

so the movedirection is from Input.GetAxis

#

and if it's over 1 a button is pressed

muted shard
#

Yes, yes

jolly crown
#

mathf.abs makes it 1 so the number from input.getAxis is always 1 or 0

#

i think

muted shard
#

Ohh, that is the input, not the speed

#

Ok

jolly crown
#

yes

#

the problem is now that when I change direction the "currentSpeedX" stays on 500 .. so no acceleration when changing direction

muted shard
#

I see

#

Shouldn't currentSpeedX be clamped between -speedMax and speedMax?

#

Instead of 0 and speedMax

jolly crown
#

ok.. I 'll try

muted shard
#

And maybe you should check the velocity on the Rigidbody2D as you test, not only on the script

muted shard
jolly crown
#

yeah the player moves to the opposite direction from key press now and keeps moving

#

no way to stop it

muted shard
#

By the way, I think decceleration should be higher than acceleration. Can you try this?

jolly crown
#

ok I set acceleration to 1500 and deceleration to 2000 but still the same problem ..

jolly crown
#

in another way?

muted shard
#

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)

jolly crown
#

oof do you know how to apply deceleration when changing directions.. I can't find any helpful tutorials online /:

muted shard
#

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

jolly crown
#

ew the turtle.speed gives n error do I have to set sth up for that?

muted shard
#

Sorry, turtle.speed.x

jolly crown
#

mmh it says the rigidbody doesn't have a definition for speed 😱

muted shard
#

Soooorry

#

turtle.velocity.x

jolly crown
#

everthing fine (;

muted shard
#

😅

jolly crown
#

ok just to go sure again
I will remove that and put the other code in?

muted shard
#

Remove just the upper half, for velocity x. Then you'll have to do the same for velocity y but finish this first

jolly crown
#

so like this?

#

I've tried around with that but there the turtle can't move in x

muted shard
#

Add even the rest. So below what you added, add:

...
else{
   if(moveDirection.x < 0){
   currentSpeedX -= acc * Time.deltaTime;}
   else{
   currentSpeedX += dex * Time.deltaTime;
}
}
jolly crown
#

Ahhhh the currentspeedX doesn't stop increasing now ;D

#

this should be right?

muted shard
#

Just remove movedirectionforcalculatingX in the multiplication

#

Aaand, you shouldn't have removed the clamping

jolly crown
#

but the turtle is always moving a bit

#

is it possible to stop that?

#

lol now both work

muted shard
#

So is it working?

jolly crown
#

Yes only the player always moves slightly

muted shard
#

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

jolly crown
#

like this?

muted shard
#

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.

jolly crown
#

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

muted shard
#

Maybe due to the huge values of acceleration and decceleration?

jolly crown
#

I will try

#

if that fixes it

jolly crown
#

it's always blinking between some numbers

muted shard
#

That happened to me as well, but e-07 is super small so no big deal

jolly crown
#

OK

#

(;

#

Thank's a lot

#

that should be it

muted shard
#

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.

jolly crown
#

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

muted shard
#

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)

jolly crown
#

A velocity value in this rigidbody2d?

muted shard
#

Yes, under Info I think

jolly crown
#

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 🤩

muted shard
#

I mean that you can check as it jiggles what is actually happening to the velocity to find out why it might be happening.