Godot Version
V4.2.1
Question
I’m using CollisionShape2D.shape.get_rect().size.x
to get the width, but it’s returning 101, which doesn’t match the actual width of the shape. Any idea why this is happening?
SkeletonShield_mob.gd:
extends CharacterBody2D
@onready var mobArea = get_parent().get_node('CollisionShape2D')
var direction = 'right'
var speed = 500
func _physics_process(delta):
velocity.x = 200
print(mobArea.shape.get_rect().size.x)
position.x = (mobArea.shape.get_rect().size.x/2)
move_and_slide()
1 Like
Does MobArea1 or it’s CollisionShape2D have an x scale other than 1.0?
If you select the CollisionShape2D you can click into the RectangleShape2D resource and confirm the size of the shape. This value should representative of what get_rect().size
is returning, and this value shouldn’t change if the scale is being changed to make it larger.
1 Like
I set this up:
And did this:
extends Node2D
@onready var collision_shape: CollisionShape2D = $Area2D/CollisionShape2D
func _ready() -> void:
print(collision_shape.shape.size.x)
print(collision_shape.shape.get_rect().size.x)
And the output was as expected:
1001.0
1001.0
So I am guessing your collision shape is 101 wide at the moment. Are you using any camera zooms perhaps? Or a strange shape like a polygon or circle? Try in the debug settings setting collision shapes visible and see what it looks like in your game. That might help identify your problem.
PS Are you sure you are referencing the correct shape?
PPS @withersail raised a good point. Parent node scaling might be an issue too.
2 Likes
Yeah, that worked! The original size of the RectangleShape2D was 101. So do I always have to set the size through the RectangleShape2D?