- Home /
How to rotate a gameObject towards a hit point along Z axis?
my camera is on Z axis.. all of my objects are on the x-y pane.
so, i have a game object with an arrow sprite pointing up. I want to have that arrow head pointing towards the hit point.
I have a function that is called upon Raycasthit is known:
Quaternion toHitRotation = Quanternion.FromToRotation(hit.point, Vector3.forward) * this.transform.rotation;
this.transform.rotation = toHItRotation;
however, it doesn't work. I check the gameobject, instead of having x & y being zero, now, there are values in x & y, and things get weird. I tried to "normalized" by adding:
this.transform.rotation = this.transform.rotation * Quanternion.Euler(0,0,90);
and still doesn't work.
what did I do wrong?
Is your camera position (0,0,0) ? Because putting a point position (hit.point) gives in fact the vector between (0,0,0) and your point. What you want to do is something like hit.point - Camera.main.transform.position
(this assumes that the camera your using has the tag "$$anonymous$$ain Camera").
An other (and easier imo) solution is to use 'just' transform.LookAt(hit.point);
on your gameobject and job is done :) You may have to add an offset to the rotation (like +180 degrees) if your gameobject is not facing the good direction.
Your answer
Follow this Question
Related Questions
Rotate animation z coordinate to Equal Targets z Rotation. 0 Answers
Problem with Rotate() conditions 0 Answers
,The object rotates in z axis unwantedly 2 Answers
Continuously rotating by 90 doesn't get me the initial angle 0 Answers
Lock Z rotation to 0 2 Answers