Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | jkunable |
In my 3D project, I am moving a kinematicbody from its current location to a specific point. When the kinematicbody is a certain length away from that point, I directly set its position to the point’s position; position = point
The Godot docs warn against setting a kinematicbody’s position directly, but for my purposes, I need the kinematicbody to be set exactly to the target point. So, is my approach okay? If not, what are the drawbacks and is there a better way to achieve what I want? Thanks in advance. Code example:
func _physics_process(delta):
velocity = (target - position).normalized() * speed
if (target - position).length() > 0.5:
velocity = move_and_slide(velocity)
else:
position = target
Setting the position and using translate for movement bypasses the physics. it’s basically so collisions/interactions behave better.
Magso | 2019-06-25 20:09
So when I set “position = target” in the code snippet above, are you saying that if something were to collide with it during that assignment it could cause errors or are you saying that when I directly set the position, the physics is bypassed, therefore no physics error will occur?
jkunable | 2019-06-25 22:36
No, it’s ok to set it’s position but you said “The Godot docs warn against setting a kinematicbody’s position directly.” This means it’s better using move_and_slide
instead of translate
, for example the docs for move_and_slide
say “If the other body is a KinematicBody or RigidBody, it will also be affected by the motion of the other body.” translate
would not allow for this as it alters the position directly.
Magso | 2019-06-26 00:34
Oh okay, I completely understand what you’re saying. I’ll have to run some test to see if being slightly off from the target is acceptable or if working around that drawback is the best choice of action. Thank you for your help. So, moving a kinematic body to a precise location isn’t possible through the use of move_and_slide.
jkunable | 2019-06-26 02:14