I am currently converting a 2D game to 3D from gamemaker. One thing I struggled with is getting objects to point towards the mouse. From my understanding, you must use raycasting for 3D mouse pointing. It is fine when I use the look_at function like this:
KinematicBodyNode.look_at(cursor_pos, Vector3.DOWN)
But the disadvantage to this code is that it rotates the CollisionShape because it is a child of the KinematicBodyNode. So what I decided to do was to use the function like this (because I only want to rotate the mesh that is a child of KinematicBody):
MeshInstanceNode.look_at(cursor_pos, Vector3.DOWN)
This now no longer rotates the CollisionShape node but the MeshInstance now which is what I want. But the problem is now that I only want it to rotate by the y axis (because it is meant to be a flat sprite with x axis rotated at 90 degrees so that it is lying on the ground).
I found a quick hack to prevent this problem (by setting the rotation x back to -90 after using 'look_at'). This is works exactly how I want it to except how do I do this the non-hacky way.
This is how I (kind of) got it to work:
MeshInstanceNode.look_at(cursor_pos, Vector3.DOWN)
MeshInstanceNode.set_rotation(Vector3(-90, MeshInstanceNode.get_rotation().y + 90, 0))
But it still has problems like a slight unwanted rotation and the rotation is a bit off but the closest I have managed to getting it to work like when I was just getting its parent (KinematicBodyNode) to rotate.
But why does this code not work the same? It doesn't seem to put the correct value in the angle variable but surely this should be the equivilent to the angle which 'look_at' provides:
var angle = cursor_pos.angle_to( MeshInstanceNode.global_transform.origin)
var mesh_rot = MeshInstanceNode.get_rotation()
mesh_rot.y = angle
MeshInstanceNode.set_rotation(mesh_rot)
Another question:
Is there a better way I could place meshs into my level (were I do not even have to set the rotation y to -90). Like say if I imported from Blender? Or is that too much work? I just thought it might make more sense if it looked like a flat sprite on the floor with rotation values all at 0, 0, 0.
Also (the code above shows me having to offset the y rotation by 90 degrees (I guess I could make it so the sprite texture is 90 degrees to the right but is there not a better way to rotate this in the editor without having to hardcode the offset rotation).
Thanks