3D Physics Collision Signals

ay up,

OK, so I seem to be struggling getting my head around how collision information can be gleaned between two objects, as in if A collides with B, let a function in scriptX know about it and act on it.

what i ultimately want to have is when an obstacle collides with the player, i can capture that collision in a script somewhere and do something with it ( ie decrease health, remove the obstacle instance the same way as the area collisions work) so here what I currently have, and as you can see the collisions do work, i just cant figure out how to tie the signals/script in together so i can remove the obstacle once it collides with player.

as a brief description of what i have, its a little game im trying to create from scratch, much like temple run, where the player and ground are stationary and the obstacles come down the lane towards player.

firstly here is my Scene tree in the main level.
image

here is the player scene

the player and obstacles are instantiated scenes (player doesnt have to be) , obstacles are for instancing purposes.

heres the obstacle scene
image

the obstacles are spawned at the end of the lane and travel towards the playerimage
at the rear behind player is an Area Collision Object.
when the obstacles collide with this they are freed up. this part functions correctly.
as the Area has a body_entered signal that is attached to a script on Spawners
image

So ideally I would have made the player a KinematicBody rather than a RigidBOdy set to Kinematic - you get some handy built in functions like get_collision() which wold be perfect.

However, this can be done with a RidigBody with a bit of fiddling.

First, you need to make sure that the Player is actually checking for collisions rather than just interacting with them. To do that, head to the inspector and make sure Contacts Reported is set to something above 0 (if the player is colliding with the floor, you’ll want this set to 2 or higher) and that Monitor Contacts is on.

That’s going to let you use get_colliding_bodies() - if memory serves that’s going to return an array.

Or, even easier, set up a signal from the Player to the Player - you’ll want on_Body_entered(body)

Then in the Player you can do this:

func on_Body_entered(body):
   if body has_method("my_method"): 
        some_function_I_want_To_call()
        body.queue_free()

I put has_method(“my_method”) because it allows us to see if whatever is colliding with the player has a method in a script with than name, which means we’ll only interact if they do. You can also do this with has_node(“a_node”) and give the object a child node, or by lots of other methods (likebody.name == "bob" or body is RigidBody)

See how you get on from there!

ended up leaving it as a RigidBody and locking axis.

you would have thought that if you up the contacts to report, that the contact monitor would enable at the same time grr, clean forgot to check that.
image
ok seems to be working now, so i can move on. thats taken a good chunk of the day getting my head around that. tricky coming from one way of doing things and having to unlearn and learn and try not to compare workflows all the time :wink:

ill get there, now to get the scoring and UI done, along with moving the player heh

hopefully in time for Marcs deadline tomorow night to get a somewhat functional prototype out in the wild.

Cheers Yann

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms