- Home /

# 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.

**Answer** by Waz
·
Aug 30, 2011 at 10:56 PM

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.

**Answer** by aldonaletto
·
Aug 30, 2011 at 11:34 PM

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.

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! :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

If you are a new user, check out our FAQ for more information.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

For troubleshooting common problems with Unity 5.x Editor (including Win 10).

### User Guide

To help users post good questions and use the site effectively we have posted a user guide. Please check it out.

### Follow this Question

### Related Questions

Camera Loop Rotation 0 Answers

2D plane physics, add torque based on direction 1 Answer

Rotation via RPC and wrong result angle 1 Answer

How to calculate angles and apply to rotate an object. 1 Answer