Rigidbody collision detect which collider was hit?
Asked Answered
C

4

0

I shoot a shell and it collides and that works great except for one thing. The "body" in the entered signal is the object that contains the collider rather than the collider itself. I have several colliders in the object and what to do is determined by which one was hit.

Is there a way for the projectile to determine the collider or a way for the collider to identify itself? (I can process from the collider itself, that would actually be more convinient)

Creamer answered 8/2, 2023 at 1:14 Comment(0)
M
0

Take this with a grain of salt but IIRC you might have to call on the physics server to determine which nodes/colliders are colliding. Alternatively I think upon collision the physics server might be creating a collision object, that known what and how exactly have collided. Looking at documentation in more detail should tell you more.

Looking at the docs, I may have been thinking specifically of these:
https://docs.godotengine.org/en/stable/classes/class_physicsshapequeryparameters.html
https://docs.godotengine.org/en/stable/classes/class_physicstestmotionresult.html

Meatus answered 8/2, 2023 at 3:17 Comment(0)
C
0

While those look interesting I have no idea how to access those. AFAIK I only have access to the target base node during the on<myobj>body_entered() event

Creamer answered 8/2, 2023 at 6:58 Comment(0)
C
0

Ok after much expermenting (with eyes closed in the dark) I found out how to find which collisionshape was hit.

First I changed the projectile back to a KinematicBody then in the script I moved it with move_and_collide which gave me a kinematiccollision object when it hit something. From there I used collider_shape and that game me the exact collisionshape that was hit! I moved like this:

func _physics_process(delta):
	velocity = startTranform.basis.y
	velocity.y += gravity * delta
	var col = move_and_collide(startTranform.basis.y, false)
	if col:
		print(col.collider_shape)
		free()

Now I'll just assign col.collider_shape to objectHit and use methods inside it that I write in order to deal damage. That took quite a while to figure out!

Creamer answered 8/2, 2023 at 8:46 Comment(0)
M
0

I totally forgot to consider you might be using move_and_collide. Yes. Sometimes the most obvious solution is the easiest to overlook! 😥

Meatus answered 8/2, 2023 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.