#2d character movement
1 messages · Page 1 of 1 (latest)
A tool for sharing your source code with the world!
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
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
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
ahh ok ✅
now they are fixing it to stop relying on modifying the transform for movement since that ignores physics
almost, reread what i said carefully
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
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
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
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
oh ok then its fine
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)
but the code i had at first couldn't that change direction with a key press
anyway, now that it's actually moving you can change the Flip method to work with your movement
ok
basically just swap the movementInputX variable in your Flip method with movement.x
i got alot of errors doin that
wanna share the errors and code?
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)
A tool for sharing your source code with the world!
this should be right
i'm getting no errors
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
but when i move the rb.AddForce(movement * speed); i just get a error saying that movement isnt valid
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
i think i made it work
A tool for sharing your source code with the world!
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
oh ok
you mean that i should change this public int Direction = 1;
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
like this https://paste.mod.gg/gislhhpjvvfg/0
A tool for sharing your source code with the world!
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;
}
}
how do i make my character not slide on the ground
set up your mass and linear drag. maybe also give your walkable surfaces a material with some friction
can u help me make my gun shoot rubber ducks that explode on impact
@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
ok