Topic was automatically imported from the old Question2Answer platform.
Asked By
inTech
Old Version
Published before Godot 3 was released.
Hey people
So I have two scenes set-up. One to spawn the player and another one as the level. The player is KinematicBody2D node and the level’s object is RigidBody2D node. I have CollisionShap2D on all my objects and currently they are able to move each other when they collide.
However, I cannot detect collision via GDScript. I tried is_colliding() function and it never return true. I searched for similar question already and one of the replies was that is_colliding only works if I have move() function inside my KinematicBody2D object.
Now, my question is how do I detect collision without using the move() function?
bool test_move ( Vector2 rel_vec )
Return true if there would be a collision if the body moved in the given direction.
Thanks for the quick response! This simple and handy trick worked for me!
if (test_move(Vector2(get_pos().x, get_pos().y))):
inTech | 2016-12-13 09:01
I don’t think that the get_pos() Vector is the right choice for this purpose.
The test_move function expects a direction (documentation).
For example, if the test_move(Vector2(0,1)) returns true, it means, that your kinematicbody can not move downward (in a side-scrolling game).
But I’m a beginner… so it is not completely sure
bruteforce | 2016-12-13 10:19
Oh thanks details. After some testing with the previous script, there is indeed a logic bug.
I changed my code to the following and it seem to return true only when its collide with an object above themselves.
if (test_move(Vector2(0, -1)) || test_move(Vector2(1, -1)) || test_move(Vector2(-1, -1))):
I am not sure if I need the last two OR conditions or not. I think it really depend on how my game works. I will be testing it and see which one blend in with my game better.