0 votes

I am trying to rotate a kinematic body and then check if the body is colliding using the moveandcollide() function, with 'test_only' as false. However, whenever i attempt to get collision information, it returns an 'invalid get index' or 'null instance' error (depending on how i'm getting this information)


Here is the code:

func _physics_process(delta):
    CNTRL() #does the movements and stuff
    var testcol = move_and_collide(Vector3(0, 0, 0.1), false, false, true)
    if testcol.testcol.collider().get_collider_id() == "world": # if colliding with the object (in theory)
        print("colliding") #return

From what i've read everywhere else, i'm not doing anything wrong. Wondering if maybe i've messed up some syntax? Used .collider() and .collider so i dont think its that though.

Godot version 3.2.2. stable
in Engine by (12 points)

1 Answer

0 votes

move_and_collide() returns a KinematicCollision object if there's a collision. When you move and there's no collision, it returns null, which is exactly what your error says.

As explained here:
https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html#detecting-collisions

You must check if there even was a collision before trying to access properties of that collision.

There are other problems with your code:

  • You wrote testcol.testcol and the KinematicCollision doesn't have a testcol property
  • collider is a property of KinematicCollision, not a function
  • collider_id is also a property, but it's an int, and so could never be equal to "world"

This could work (not knowing exactly what you're trying to check):

if testcol and testcol.collider.name == "world"
by (21,714 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.