#Need help with player movement

1 messages · Page 1 of 1 (latest)

hybrid granite
#

hello I'm very new to godot 4. I'm making a 2D side scroller, but I can't seem to figure out how to make my player character change animations when they are idle/walking/sprinting (also, I can't figure out how to make them sprint)

formal pewterBOT
#
How to ask for help

Following these tips will increase your chance of getting good answers:

Include Details

Helpers need to know as much as possible about your problem. Try answering the following questions:

  • What are you trying to do? (show your node setup / code)
  • What is the expected result?
  • What is happening instead? (include any error messages)
  • What have you tried so far?
Respond to Helpers

Helpers often ask you questions to better understand your problem. Not answering them or saying "it's not relevant" is not helpful. Even if it is not directly relevant, there is a high chance the answer will give more context for the people that are helping you.

Don't be demanding

Please don't expect people to immediately help you. We all do this in our freetime. It may take some time until someone who can help you will answer.

hollow trail
hollow trail
#

line 23 is incomplete. should probably at least be $AnimatedSprite2D.play("walk_R")

#

However, I think there's a better way to handle player movement animations here. Gimme a sec

#

Wait... actually. I think this is mostly right, but I think line 23 is actually causing the issue here. You're telling the animated sprite to play in line 16, and 19. Those actions are getting overwritten by line 23. Delete that and see if it works better for you

hybrid granite
#

it seems to be working better now, thanks!
but the player sprite isn't moving...how do I fix this?

formal pewterBOT
#
Embedding code in discord messages
Inline Code

When you surround some words with single backticks like `this`, it will be formatted as code.

Code Blocks

You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:

print("hello world")
Upload

If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/

hybrid granite
hollow trail
hybrid granite
#

characterbody2D

hollow trail
#

Okay, perfect. Here's what you need to change:

#rename this to use physics:
func _physics_process(delta) -> void:
  # rename this variable We already have velocity in the class.
  var direction = Vector2.ZERO
  if Input.is_action_just_pressed("move_R"):
    direction.x = 1
    $Anim.... #this line is fine. keep it as is
  if Input.is_action_just_pressed("move_L"):
    direction.x = -1
    $Anim... #this line is fine. keep it as is
  if direction:
    velocity = (direction * SPEED).normalized()
  else:
    velocity.x = move_toward(velocity.x, 0, delta)
    velocity.y = move_toward(velocity.y, 0, delta)
  # check if we're moving
  if not velocity:
    $AnimatedSprite2D.play("idle_R")
  

This should be a good start. Do you need me to go more in depth on what's going on?