#2d character movement

1 messages · Page 1 of 1 (latest)

limpid parrot
#

show the code as it is now

merry apex
limpid parrot
#

you should be polling for input in Update but actually moving it in FixedUpdate

One way you could do that would be to move lines 31 and 32 back into update then make your movement vector a class level variable and set it in update to be used in FixedUpdate

#

after making that change you can change your Flip method to use momvent.x and movement.y instead of movementInputX/movementInputY since you aren't even setting those anywhere

sterile solstice
#

Tbh the only thing he’s asking for is to make it rotate left and right when he presses a or d? He could just add a snippet of code that rotates it facing left or right under each button press

#

So like if button input a > move left + face left

limpid parrot
#

that was the original issue and they were just handed code that does it for them but they blindly copied and didn't actually change anything

sterile solstice
#

ahh ok ✅

limpid parrot
#

now they are fixing it to stop relying on modifying the transform for movement since that ignores physics

merry apex
#

is this what u meant

limpid parrot
#

almost, reread what i said carefully

merry apex
#

then make your movement vector a class level this is what im missing

#

right

limpid parrot
#

that and assigning its value in Update where you are getting your input values

#

that way you can assign its value in Update and access it in FixedUpdate

merry apex
#

don't i already have a value

#

public float speed = 7f;

limpid parrot
#

that's the speed value, not your movement vector though

#

you are multiplying your movement vector by the speed value to determine how much force to apply and in what direction

merry apex
#

but i dont want it to be a force because now the character doesn't stop when i stop pressing the button

#

and its accelerating

limpid parrot
#

well there were other options to choose from. but you can also just set up the rigidbody2d mass and drag and it won't do that

#

i like to use 0.6 for the mass and about 3 for the drag, then about 4 for the speed but that still gives a tiny amount of slide (which was intentional for what i was building) but you can change those values to whatever you like

merry apex
#

oh ok then its fine

limpid parrot
#

or you can use a completely different option for moving your player such as setting velocity directly or using the MovePosition method (although that is intended for kinematic rigidbodies)

merry apex
#

but the code i had at first couldn't that change direction with a key press

limpid parrot
#

anyway, now that it's actually moving you can change the Flip method to work with your movement

merry apex
#

ok

limpid parrot
#

basically just swap the movementInputX variable in your Flip method with movement.x

merry apex
#

i got alot of errors doin that

limpid parrot
#

wanna share the errors and code?

merry apex
#

ok

limpid parrot
#

well now you are adding force in Update instead of FixedUpdate like you should be, and you didn't make movement a class level variable it's still a local variable hence the errors in the Flip method

#

you also just copy/pasted movment.x instead of seeing that it was clearly a typo meant to be your already exisiting movement variable accessing the x property

#

oh and you also have both a bool called facingright and an int called isFacingRight when you really only need the bool (and you need to make the change in your Flip() method to use the bool instead of the int)

merry apex
#

this should be right

#

i'm getting no errors

limpid parrot
#

it's not

#

you just ended up creating a float movment instead of fixing it the way i told you to, you also still have your AddForce call in Update instead of FixedUpdate

merry apex
#

but when i move the rb.AddForce(movement * speed); i just get a error saying that movement isnt valid

limpid parrot
#

that's because you still haven't made your movement vector a class level variable

#

right now it is a local variable whose scope is entirely within the Update method so it cannot be accessed in other methods

merry apex
#

i think i made it work

limpid parrot
#

nice! it looks mostly okay to me. I still think you should change to using the bool instead of the int in your Flip method

merry apex
#

oh ok

merry apex
limpid parrot
#

no, i don't think you even need that variable. I mean swapping isFacingRight == 1 with your bool facingright

#

i also think you should actually rotate the player around the y axis or use the flip x property on the sprite renderer instead of using negative scale but that's honestly just a nitpicky thing and negative scale should (theoretically) work fine

merry apex
limpid parrot
#

almost,you want the second one to be false so invert it with ! so it turns into !facingRight in the if statement. Then you'll also need to change the value within the blocks so change isFacingRight = -1 to facingRight = false

#

making those changes turns the Flip method to this:

 public void Flip()
    {
        if (facingRight && movement.x < 0)
        {
            facingRight = false;
            Vector3 scale = transform.localScale;
            scale.x = -scale.x;

            transform.localScale = scale;
        }
        else if (!facingRight && movement.x > 0)
        {
            facingRight  = true;
            Vector3 scale = transform.localScale;
            scale.x = -scale.x;

            transform.localScale = scale;
        }
    }
merry apex
#

how do i make my character not slide on the ground

limpid parrot
#

set up your mass and linear drag. maybe also give your walkable surfaces a material with some friction

merry apex
#

can u help me make my gun shoot rubber ducks that explode on impact

merry apex
#

@limpid parrot

limpid parrot
#

plenty of tutorials for how to do that. if you have a specific issue you can always ask about it in the #💻┃code-beginner channel so that anyone who sees it can help you

merry apex
#

ok