Global direction (e.g. Vector3.forward, Vector3.right) from Mouse drag
I'm trying to get the Global direction (I'm sorry if that's not the correct term, please let me know) as in Vector3.forward and Vector3.right from a Mouse drag.
However, the camera angle will change, but I want the player to be able to swipe in a direction and see if that's more 'forward' than 'right' according to my GameObject, for example. (In my case, my object can only move forwards, backwards, left and right on a grid-like pattern)
Here's my code so far, but I get stuck :
private void OnMouseDown()
{
Camera camera = Camera.main;
on_click_position = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
}
private void OnMouseDrag()
{
Camera camera = Camera.main;
Vector3 cursorPosition = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
Vector3 heading = cursorPosition - on_click_position;
Vector3 direction = camera.transform.TransformDirection(heading / heading.magnitude);
if (float.IsNaN(direction.x))
{
return;
}
float forward = Vector3.Dot(direction.normalized, Vector3.forward);
float right = Vector3.Dot(direction.normalized, Vector3.right);
}
Forward seems to work, but I don't know why the Vector3.Dot result of my Vector3.right calculation gives me outlandish results like '-9.536744E-07'
Any ideas? What am I doing wrong?
Your answer
Follow this Question
Related Questions
converting local space vector into world space? 1 Answer
Unity2d worldspace / local space vector 3 help needed 0 Answers
How to translate World <->Local position without Transform component with Matrix4x4? 0 Answers
Keeping worldspace when parenting to elevator 1 Answer
Camera movement problem 0 Answers