Before updating to Windows 10 you may need to return your Pro license. More information here

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

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.

alt text

more ▼

asked Aug 30, 2011 at 09:16 PM

avatar image

467 66 72 66

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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, 2011 at 10:56 PM

avatar image

7.8k 68 103 116

(comments are locked)
10|3000 characters needed characters left

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, 2011 at 11:34 PM

avatar image

61.6k 57 213 361

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, 2011 at 11:39 PM Tonzie
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Aug 30, 2011 at 09:16 PM

Seen: 1307 times

Last Updated: Aug 30, 2011 at 11:39 PM