- Home /
Question by
UDN_c0005ce8-994e-4584-a2ec-402373921f84 · Aug 15, 2017 at 12:55 AM ·
vector3directionforwardwrong
Problems to project one vector over another
I need help, I could not project the same direction of the mouse (green) position on the forward direction of my gamobject, since to put 0 in Y to give the direction of forward gives an incorrect direction. Someone could help me I would appreciate it a lot.
Plane plane = new Plane(mainCamera.transform.forward * -1, bulletPos.transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float rayDistance;
if (plane.Raycast(ray, out rayDistance))
{
Vector3 dir = (ray.GetPoint(rayDistance) - transform.position);
Vector3 dirForward = new Vector3(dir.x, 0,dir.y).normalized;
var rotation = Quaternion.LookRotation(dirForward);
transform.localRotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 13);
Debug.DrawRay(bulletPos.transform.position, dirForward * 100, Color.blue);
Debug.DrawRay(transform.position, dir, Color.green);
}
pojection-vector.png
(5.0 kB)
Comment
Your answer
Follow this Question
Related Questions
"Fake" Vector3.forward? 3 Answers
my rocket fires in the wrong direction 4 Answers
Raycast in the wrong way 3 Answers
My bullets arent going the right direction 1 Answer
Can't change vector direction 0 Answers