# How do I create a shape that rotates from one pivot point, to always point towards the mouse cursor?

 0 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. more ▼ asked Aug 30 '11 at 09:16 PM Tonzie 178 ● 16 ● 20 ● 21 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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. more ▼ answered Aug 30 '11 at 11:34 PM aldonaletto 42.5k ● 16 ● 43 ● 202 The pivot rotation is in Z, and Y is the axis that will be point to the mouse cursor. I'm using C#, so I'll translate it and post it in C# if it works. Thanks! :) Aug 30 '11 at 11:39 PM Tonzie add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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. more ▼ answered Aug 30 '11 at 10:56 PM Waz 6.5k ● 23 ● 33 ● 71 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x2249
x1002
x266

asked: Aug 30 '11 at 09:16 PM

Seen: 640 times

Last Updated: Aug 30 '11 at 11:39 PM