- Home /
How do I reset the mousePosition relative to an object I click on?
Hi there, As the title suggests, I would like to rotate a platform (that balls bounce off). At the moment it rotates, but is uncomfortable as the platform rotation is equal to the Input.mousePosition.x.
eg. When I would like the move the platform 20degrees, the mouse.x will be at 175, and I need to make is 20 so that it rotates correctly.
Here is the code.
var lastMousePosition : Vector3;
private var shouldRotate:boolean;
function Start(){
shouldRotate=false;
}
function Update() {
Debug.Log(Input.mousePosition);
}
function FixedUpdate(){
if(shouldRotate){ //If shouldRotate = true...
if(Input.GetMouseButton(0)){ //and if MouseButton is clicked
if(Input.GetMouseButtonDown(0)){ //and held down
//reset
lastMousePosition = Input.mousePosition;
}
else {
transform.root.transform.Rotate(0,0,Input.mousePosition.x - lastMousePosition.x);
}
lastMousePosition = Input.mousePosition;
}
}
}
function OnMouseUp(){
shouldRotate=false;
}
function OnMouseDown(){
shouldRotate=true;
}
Answer by robertbu · Apr 30, 2013 at 01:31 PM
Your question is a bit unclear. You are using the mouse x start position minus the end position for your rotation, so I'm going to assume that if you have a movement of 175 screen units of the mouse you want a rotation of 20 degrees:
if (Input.GetMouseButton(0) {
transform.root.transform.Rotate(0,0,(Input.mousePosition.x - lastMousePosition.x)*.1143);
lastMousePosition = Input.mousePosition;
}
A couple of observations. Your comments are backwards. GetMouseButton() returns true for every frame the mouse is held down. GetMouseButtonDown() only returns true for the frame the mouse buttons is pressed down. The GetMouseButtonDown() clause is not doing anything since you also set 'lastMousePosition' on line 23. Your code does not need to be in FixedUpdate() and instead should be in Update(). FixedUpdate() is typically used with Ridigbodies.