- Home /
Rotate plane on a pivot to the mouse position (in 2D)
Hello unity community again!
I have a more rudimentary question now, however, I am struggling to find a solution.
I have a 2D player sprite on a plane, all rigged for sprite animation and movement. However, I would like to add a black coloured plane just behind that will simulate his other hand. This hand is meant to follow the mouse.
However, No matter what script or function i use, his arm never follow the mouse properly and often disort it (as i have it in another object ot get a new pivot.)
Is there any way to fix it?
//var tmpPos : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (Input.mousePosition.x,Input.mousePosition.y,0));
//MousePositionWorld = Vector3(-tmpPos.x,-tmpPos.y, transform.position.z);
//Arm.rotation = Quaternion.FromToRotation(transform.position, -MousePositionWorld);
//Arm.LookAt(MousePositionWorld, Vector3.up);
var mousePos = Input.mousePosition;
// get the mid-point distance between camera and object
mousePos.z = transform.position.z;//Vector3.Distance(Camera.main.transform.position, transform.position)/2;
// look at this world point
Arm.LookAt(Camera.main.ScreenToWorldPoint(mousePos));
Here is a picture to help you understand on what exactly I want to do.
![alt text][1]
Thank you for your help in advance, hope it is resolved soon. -Lachee [1]: /storage/temp/6886-answerquestion02.png
Answer by sparkzbarca · Jan 20, 2013 at 07:43 AM
Arm.rotation = Quaternion.FromToRotation(transform.position, -MousePositionWorld);
this isn't right
FromToRotation (FromDirection, ToDirection)
DIRECTION
not position.
your position is the not the same thing as your facing. Your facing can change without having changed your position.
use transform.RotateAround(point, axis,angle)
for this next part
point = point as which the arm connects with the body, the pivot point. You'll have to pick a point
A = whatever direction your starting from or in this case A = point on the plane you want to end up where the mouse is - point
B = where you want to end up or in this case B = mouseposition - point
axis = vector3.cross(A, B);
angle = (i think) mathf.atan2(Magnitude(axis), vector3.dot(A, B));
Best of luck.
Mark as answered.
Your answer
Follow this Question
Related Questions
How to rotate an object by its pivot in script 3 Answers
Mouse position not returning actual position 0 Answers
Change Sprite Origin in 2D 1 Answer
Rotation of GameObject w/ no movement 2 Answers
How to position objects with mouse? 2 Answers