I have a plane that is set on a pivot point (model inside a game object set at the pivot point) that I would like to rotate to always face my mouse. How would I go about doing this?
I’ve looked into Transform.LookAt() and it doesn’t apply in this situation because the plane needs to look at the mouse in the Y axis.
How do I create a shape that rotates from one pivot point, to always point towards the mouse cursor?
LookAt makes Z face a direction. Simplest solution is to change your plane so that Z is perpendicular, eg. by wrapping it in an empty parent object and rotation the plane relative to the parent.
You must create a plane (a math plane, not the mesh) at the pivot point, and perpendicular to its axis; create a ray from the mouse pointer, and use Plane.Raycast to find the point where the mouse ray hits the plane; use GetPoint to get a 3D point at this position, then finally create a rotation to look at its direction.
I have no enough information to know which axis you’re using in the pivot, and I’m not sure if Y is the axis which should look at the mouse pointer. For this reason, I defined these axes in the form “Vector3(x,y,z)”, where the axis selected is 1 and the others 0; modify this if you need:
function Update(){ // Define the pivot axis as Vector3(1,0,0) (the X axis) var plane = Plane(Vector3(1,0,0), transform.position); var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var distance: float; if (plane.Raycast(ray, distance)){ var direction = ray.GetPoint(distance)-transform.position; // create rotation from Y axis to the mouse pointer direction var rot = Quaternion.FromToRotation(Vector3(0,1,0), direction); transform.rotation = rot; } }
I haven’t tested this; let me know if you have any problem.