Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | alann |
I want the user to be able to rotate an object in any direction by clicking and dragging.
I have managed to get a mouse vector of the move that tells me the direction the user dragged:
extends MeshInstance
var mouse_start = Vector2(0,0)
var mouse_end = Vector2(0,0)
var dragging = false
var speed = 0.5
func _input(e):
if e is InputEventMouseButton and e.is_pressed():
mouse_start = e.position
dragging = true
if e is InputEventMouseButton and not e.is_pressed():
mouse_start = Vector2(0,0)
mouse_end = Vector2(0,0)
dragging = false
if dragging:
mouse_end = e.position
func _process(delta):
if dragging:
var mouse_dir = Vector2(mouse_end.x - mouse_start.x, -mouse_end.y - -mouse_start.y)
mouse_dir = Vector3(mouse_dir.x, mouse_dir.y, 0).normalized()
#how to apply relative to camera?
But not sure how to apply this to the rotation correctly so it’s relative to the camera?
I have tried to rotate(mouse_dir, delta * speed), but the cube disappears, not sure why (it prints it’s in the same place, same scale, same rotation)? I get the following error. But using a something like Vector3(0,1,0) or even a Vector3 with lots of decimals like the mouse_dir, normalized, all work? Could this be a bug?
Condition ' p_axis.is_normalized() == false ' is true.