# How to only play "Walk" animation if not on screen edge? ( Using clamp function )

Attention Topic was automatically imported from the old Question2Answer platform.
``````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

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!

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!

MaybeAnonymous | 2022-03-15 08:51