Moving an object slower, in clockwise motion, around a pivot with Quaternion.AngleAxis
Hello Everyone, pls pls pls help me..
I want my object to move in clockwise motion at a slower speed, when I click, hold and drag my mouse in clockwise motion, around a pivot.
I already managed to do it, at full speed, but not at a slower speed.
When I try to make it move slower, the object does move slower, but halfway through the movement, it will go back to where it starts.
For example, when ....
Normal speed = Full clockwise motion
Slow speed = Half clockwise motion (after reaching the end of the half clockwise motion, it will go back to where it starts (I'm not sure)).
.
Here is the code, "target" is my pivot.
If I take out, ( ang = ang / 2; ), object is able to move clockwise motion at full speed.
void Update () {
//360 DEGREE
if (lastAngledifference < -180)
{
lastAngledifference = lastAngledifference + 360;
}
if (lastAngledifference > 180)
{
lastAngledifference = lastAngledifference - 360;
}
//calculate distance
if (Input.GetButtonDown("Fire1"))
{
angleDifference = 0;
}
if (Input.GetButtonUp("Fire1"))
{
lastAngledifference = lastAngledifference + angleDifference;
}
if ( (canPlay) && (canStart) && (!twofingers) )
{
if ((Input.GetButtonDown("Fire1")))
{
//mouse position at start, and calculate angle
touchPoint = Camera.main.WorldToScreenPoint(target.position);
touchPoint = Input.mousePosition - touchPoint;
deltaAngle = Mathf.Atan2(touchPoint.x, touchPoint.y) * Mathf.Rad2Deg;
}
if ((Input.GetAxis("Mouse X") < 0) || (Input.GetAxis("Mouse X") > 0) || (Input.GetAxis("Mouse Y") < 0) || (Input.GetAxis("Mouse Y") > 0))
{
//mouse position when moving, and calculate angle
pt = Camera.main.WorldToScreenPoint(target.position);
pt = Input.mousePosition - pt;
ang = Mathf.Atan2(pt.x, pt.y) * Mathf.Rad2Deg;
ang = ang / 2;
angleDifference = deltaAngle - ang;
v = Quaternion.AngleAxis( (lastAngledifference + angleDifference) , Vector3.forward) * (Vector3.right * fRadius);
transform.position = target.position - v;
}
}
}
Code Explanation:
I calculate the angle of mouse from pivot
-DeltaAngle, is calculated, when I left clicked DOWN. It calculate the angle of the mouse from pivot.
-ang, is calculated, when I left clicked HOLD, it calculate the angle of the mouse from pivot.
-angleDifference, is the angle difference of, (DeltaAngle and ang).
-Then, I move my object to angleDifference.
-lastAngledDifference, is so that, I am able to resume the clockwise motion if I stop at any point of time.
-(Vector3.right * fRadius) is how far is the object from the pivot.
Please read the Frequently Asked Questions and the User Guide.
Your question is not formatted properly.
Your question is not very descriptive: It is not clear what you try to do.
Do you want to drag (move) an object with the mouse?
Do you want to rotate an object with the mouse?
If yes, then you should google it, there is enough information about those topics. Also, you already asked this here.
If no, then be more specific and format your text properly using the tools.
What issue are you describing with
$$anonymous$$oving an object around a pivot with Quaternion.AngleAxis
This is a rotation vector, you can't move something with a rotation.
If something already works at "full" speed, then it will work at any speed. It depends on what you do with that speed.
One tip for you:
if ((Input.GetAxis("$$anonymous$$ouse X") < 0) || (Input.GetAxis("$$anonymous$$ouse X") > 0) || (Input.GetAxis("$$anonymous$$ouse Y") < 0) || (Input.GetAxis("$$anonymous$$ouse Y") > 0))
{}
..is not very efficient, basically you say "if the X or Y input is not zero":
if(Input.GetAxis("$$anonymous$$ouse X") != 0 || Input.GetAxis("$$anonymous$$ouse Y") != 0)
{}
..this also is easier to read and check for errors ;)
Have a nice day.
Your answer
Follow this Question
Related Questions
When playing builded version of game the cameras only captures part of scene. 0 Answers
Zoom camera 1 Answer
Camera Jitters When Displacing and Rotating Smoothly 0 Answers
How to rotate camera diagonally over players shoulder while still facing towards players direction 0 Answers
How to script a random asset generator> 0 Answers