Hello, rencently I started to implement to ability of throwing a knife to my game. It was working find but recently (after adding code so the knife rotate to match with its velocity) I noticed that throwing it in a corner will make it tp somewhere out of bound… I reduced the engine speed to try understanding it but I still don’t know why it happen.
I assume that little camera on the bottom right is tracking the knife, and when it turns grey that is when its going out of bounds? Are you able to post all the code related to the knife?
When you view your scene tree, does the knife still show in the tree? You can also try making the knife print its location constantly to see if its actually teleporting, and if so… where its teleporting to.
Because, to me, once it hits the first wall, it looks like it slides, but once it hits the second wall, I’m wondering if the velocity is hitting 0,0 and then the killKnife() is being called and its just being removed from the world.
you could also add a print statement to your killKnife, just make sure its the first line in the function and have it say …
The knife is still “alive” after hiting the roof, I prevent a death like that by checking if it’s also on the ground when at 0,0 velocity before killing it. And the killKnife function also hide the subviewport when killing the knife so we can be sure when it’s killed or not.
I added a way to check the position and velocity of the knife, and everything seems normal.
As the video show the knife acts as intended, it’s just invisible and with no collisions.
But my theory is false too, I checked if the 2 worlds where the same using print(knifeViewport.world_2d == get_tree().current_scene.get_viewport().world_2d)
and it always returns true.
I discover something new, it’s not doing it when hiting a corner but when hiting the roof, it don’t have to hit the wall first as in the video.
And when the knife disappear, the knifeViewport.canvas_transform.origin become (nan, nan) so this may have a link with it.
I found the solution!
The error was coming from the rotation:
rotation = atan(velocity.y/velocity.x)+PI/2
If you look closely, when the velocity.x is 0 (i.e. when hiting the roof) the code will try doing a division by 0, leading to all the horrible stuff that follow.