+1 vote
35    position.x = clamp(position.x, 0 + col_size.x/2, screen_size.x 
  - col_size.x/2)
36    position.y = clamp(position.y, 0 + col_size.y/2, screen_size.y 
  - col_size.y/2)

I have that code, and it works! But, how would I play an animation only if it's not on the edge of the screen? ( col_size is the size of CollisionShape2D, screen_size is self explanatory )

19    if input_map != Vector2.ZERO:
20      c_vel = input_map * speed_m
21      animator.play("Walk")
22  
23    # If moving horizontally
24    if input_map.x != 0:
25         $Sprite.flip_h = input_map.x < 0
26    else:
27       c_vel = Vector2.ZERO
28       animator.play("Idle")

( c_vel is the current speed, input_map is a Vector2 getting the input, speed_m is the multiplier )

I want to play "Idle" instead of "Walk" if the character is on the edge, would be nice to have a variable like that

Godot version 3.4.3
in Engine by (26 points)

1 Answer

+1 vote
Best answer

I think the most straightforward way to do this with just code would be to check how far you moved; for example:

# variables and such
# ...
func some_input_movement_handler() -> void:
    if input_map != Vector2.ZERO:
        c_vel = input_map * speed_m
        animator.play("Walk")
    # ...
    # move the thing
    # ...
    var preclamp_position := position
    position.x = clamp(position.x, col_size.x / 2, screen_size.x - col_size.x / 2)
    position.y = clamp(position.y, col_size.y / 2, screen_size.y - col_size.y / 2)

    # now check if you moved, and don't play the walk animation if you didn't
    if preclamp_position.is_equal_approx(position):
        animator.play("Idle")

is_equal_approx should be used to avoid precision errors when working with floats!

by (108 points)
selected by

That is right! But you forgot a ! before preclamp_position.is_equal_approx(position):, without the ! it was doing the opposite of what I needed, thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.