Get collider Y position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gabriel Gomez

I’m trying to get collider Y position between 2 KinematicsBody2D.

# Return collider Y position
func get_collision_ypos():
    for i in get_slide_count():
	var collision = get_slide_collision(i)
	return collision.collider.position.y

But doesn’t seems to be working.
Is there a way to get collider (not specific Node) Y position Or is not posible?


What makes you think it’s not working? Does it throw an error? Does it return the wrong value? Does it return Null instead of a value? Because, for me, your code is working perfectly fine.

njamster | 2020-02-11 22:06

I mean, the code works but is not givin a proper Y position, it’s always returning 0

Gabriel Gomez | 2020-02-11 22:21

:bust_in_silhouette: Reply From: njamster

Just to be sure: It’s returning 0, not Null? The latter would be returned when called while there aren’t any collisions. It’s not a number though, it’s literally “nothing”.

If it really is 0, check if it’s really always is, i.e. provoke a collision at a different position. Be aware, that your code does not return the position of the collision (that would be collision.position), but the position of the object you collided with (the collider).

If it really always is 0, the problem likely lies somewhere else in your project, as I could not reproduce your problem. I created a scene, added two children of type KinematicBody2D and attached the same script to them both:

extends KinematicBody2D

export var direction = Vector2.RIGHT

func _physics_process(delta):
	move_and_slide(direction * 200)

func get_collision_ypos():
	for i in get_slide_count():
		var collision = get_slide_collision(i)
		return collision.collider.position.y

I then adjusted the position of the first body to (300, 300) and the position of the second body to (600, 300), plus for the second body I set the direction to (-1, 0). If I start the scene, the bodies start moving towards each other. During that, the print-statement will output Null every frame as there are no collisions. Once the bodies collide however, it prints the correct y-position of300.

Its returning 0

Gabriel Gomez | 2020-02-12 01:17

:bust_in_silhouette: Reply From: Sween123

Is there a Node2D or something there as a parent of the KinematicsBody2D in your collider’s scene?
Sometimes I do scenes like Node2D - KinematicsBody2D - CollisionShape2D, and if that’s the case, the collider will be (0,0) since I do the position in Node2D instead of KinematicsBody2D. (Child node’s position is relative to its parent node’s position) Check how your character scene is arranged. See if it’s that you are not using the KinematicsBody2D’s position. If it’s its parent node, use collision.collider.get_parent().
collision.collider will return you the KinematicsBody2D.

My hierarchy is:


I can get the name of KinematicBody with “”, but i can’t get the position…
So, it’s pointing to KB2D

Gabriel Gomez | 2020-02-12 01:20

Put CollisionShape2D inside KinematicBody2D.
If your position is set in Node2D, use collision.collider.get_parent().position
Otherwise if it’s just the KinematicBody2d, use what you used collision.collider.position

Sween123 | 2020-02-12 01:23

Sorry, i’ve just fix the post. CollisionShape2D is inside KinematicBody

Gabriel Gomez | 2020-02-12 01:26

Okay, try collision.collider.get_parent().position

Sween123 | 2020-02-12 01:27

Doesn’t work.

Invalid get position (on base: 'Texture Rect')

Probably i should clean and start over. I’m trying to determinate if Player is on top of Enemy…

Gabriel Gomez | 2020-02-12 01:35

That means you get the wrong collider. The parent you get is a TextureRect instead of Node2D.

Sween123 | 2020-02-12 01:36

No TextureRect on the tree…so i don’t know wht is pointing to this…
Anyway, i’ve maneged to do what i was trying to do with RayCast2D:

On player:

var collider = $RayCast2D.get_collider()
if $RayCast2D.is_colliding() and collider.is_in_group("Enemy"):
	print("Enemy Killed")

On Enemy:

func get_collision():
for i in get_slide_count():
	var collision = get_slide_collision(i)

var colobject = get_collision()     

if $RayCast2D.is_colliding() == false:
	if colobject == "PlayerBody":
		print("Player Killed")

I put a raycast to down on Player and another to top on Enemy.

Thanks to all

Gabriel Gomez | 2020-02-12 09:53