Godot Version
Godot v4.7.1
Question
I’m making an infinite runner project, and I was wondering how to change the player sprite to something else when the object collides with another object. I’m using Atlas Texture with Auto sliced regions.
I have the collision code working, I was just wondering how to write the sprite change in the code as an ‘if’ statement… Thanks for the help!
if is_on_platform:
????????
player_sprite.texture = something_else
How would I go about defining ‘player_sprite’ and ‘something_else’? Both sprites are on the same sprite sheet, so it would be the same name and file?
I have no idea. I just used the terms you used in your question. Your question was too vague. If you need a more specific suggestion you’ll have to ask a more specific question and provide the exact context.
If you only want to switch the atlas region to a different frame in the same atlas texture then change the atlas texture’s region property.
Ah I see, sorry, I’m still a beginner so some questions I have might be vague.
I intend for my player object to change from an ‘alive’ sprite to a ‘dead’ sprite after colliding with a '“platform”.
Currently, I have the spikes spawning in random intervals from the edge of the screen, and the player can jump over them. I only wanted the player sprite to change to the ‘dead’ sprite after colliding with the “platform” collision box.
Currently, I have all the collision detecting code in my player scene, here’s the code if it helps
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -500.0
var is_on_platform: bool
func _physics_process(delta: float) → void:
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
if is_on_platform:
Asset_sheet_shrimp.texture = something_else
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func _on_hit_box_body_entered(body: Node2D) → void:
if body.is_in_group(“platform”) :
is_on_platform = true
In my head, it should be simple code, I just can’t seem to find anything on Google that describes what I need, so apologies for the vagueness!
As well post your sprite setup and the sprite sheet texture you use as the atlas.
The shrimp is the player, wok is the “platform” and cooked shrimp is the ‘death’ sprites.
I used the ‘auto slice’ function in ther Atlas Texture region editor.
As already noted, change the rect property of the atlas:
sprite.texture.rect = Rect2(x, y, width, height)
Where sprite is a reference to your actual sprite node and arguments to Rect2() need to be actual numbers that represent the bounding rectangle of the dead frame in the atlas.
Ah yes, I think I did try this earlier, but Godot gives me this message
Identifier “sprite” not declared in the current scope.
How would I reference the sprite node? Sorry if it seems like a stupid question 
You can get it through the node path. The path would depends on where that sprite node is in your scene tree, relative to the node that runs the script, and how it is named. So to answer that we’ll need to see your scene tree.
This is my player scene tree
From the script attached to the top node, the refence to the sprite node is $Sprite2D or get_node("Sprite2D")
So:
$Sprite2D.texture.rect = Rect2(...)
The sprite isn’t changing, which makes me think it might be my collision code that’s the problem?
Could be. Put a print statement in that code block to see if it’s getting executed when you expect it to.
Yup, my collision code isn’t working unfortunately. I used the Area2D node and the body_entered signal which should work? Unless I’m mistaken? Thank you for all your help so far by the way

body_entered signal won’t work if the platform is an area. You’ll need to use area_entered signal instead.
My ‘platform’ is a StaticBody2D though?
Then you need to check your collision layers/masks setup and make sure that the signal is properly connected to the handling function. Also enable Debug > Visible Collision Shapes to verify that colliders are actually overlapping at runtime. You can put a print statement in _on_hit_box_body_entered() as well to check if its getting called at all.
the body_entered signal is connected from the HitBox Area2D node to the main character body 2D node, which looks right to me. And the hitboxes are touching eachother at runtime, which I thought counted as overlapping? and as for the print statement on _on_hit_box_body_entered(), nothing is showing up in the console (Which is where it should show up, right?) So I’m at a loss, really…
If I could ask, in what way can the collision layer/mask setup mess up the collision detection?
Colliders won’t “see” each other if their layers/mask don’t match.