- Home /
Resetting transform's original position and rotation after using transform.rotateAround
enter code here
I have written a script which tilts the player's model (independently of the player's transfrom around a point using) using transform.rotateAround().
What's missing is that I want, at a certain point, to set back the model where it was in the beginning (but relative to the player's transform). I thought this was pretty simple but setting:
model.transform.rotation = Quaternion.Euler(...)
model.transform.position = transform.position(...) + new Vector3(...)
is behaving strangely, any ideas on how to reset back the position after using transform.RotateAround() ?
Help would be greatly appreciated, thanks in advance.
===================================================== Edit
Below I've pasted my code:
private void HandleTilting() { // print(model.transform.localEulerAngles.x + ", " + model.transform.localEulerAngles.y + ", " + model.transform.localEulerAngles.z); //print(originalModPos);
turn = currModelRotY - prevModelRotY;
if (turn > 0 && model.transform.localEulerAngles.z < 10) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 359);
if (turn < 0 && model.transform.localEulerAngles.z > 350) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 1);
//right titling
if (turn < 0)
{
rightAngle = 2 - ((2 * model.transform.localEulerAngles.z) / 40);
model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, rightAngle);
}
else if (turn > 0)
{
leftAngle = 2 - (2 * (360 - model.transform.localEulerAngles.z)) / 40;
model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, -leftAngle);
}
if (turn == 0 && model.transform.localEulerAngles.z <= 90 && model.transform.localEulerAngles.z >= 0)
{
rightAngle = (2 * model.transform.localEulerAngles.z) / 40;
model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, -rightAngle);
}
else if (turn == 0 && model.transform.localEulerAngles.z >= 270 && model.transform.localEulerAngles.z <= 360)
{
leftAngle = (2 * (360 - model.transform.localEulerAngles.z)) / 40;
model.transform.RotateAround(transform.position + new Vector3(0, 5, 0), transform.forward, leftAngle);
}
}
It has to be refined but tilting left when the button is pressed and going smoothly back works perfectly, and so does vice versa but when combining them together ...
well I know what the problem is I just need to set the local z rotation to 360 when tilting left and 0 when tilting right but for some reason the following code doesn't set the rotation
if (turn > 0 && model.transform.localEulerAngles.z < 10) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 359); if (turn < 0 && model.transform.localEulerAngles.z > 350) model.transform.localRotation = Quaternion.Euler(model.transform.localRotation.x, model.transform.localRotation.y, 1);
================
Thanks for the swift reply btw.
I edited my answer to include a possible solution for what you're trying to do - take a look at that.
Answer by aldonaletto · Sep 25, 2011 at 04:42 PM
You must first save the localPosition and localRotation of the model, do the rotation or whatever you want, then restore these local settings. If you just assign them, the model will be "teleported" to the original position/rotation; if you want it to smoothly return to the original settings, you can use the "Lerp filter":
// save the original settings (assuming model is the model transform) var rot0 = model.localRotation; var pos0 = model.localPosition; // do the movement/rotation as you're already doing // then restore them using Lerp (assuming this is in Update): var dt = Time.deltaTime * 2.3; // 2 to 5 give reasonable results model.localRotation = Quaternion.Slerp(model.localRotation, rot0, dt); model.localPosition = Vector3.Lerp(model.localPosition, pos0, dt);The actual implementation depends on your original script. If you can't find the best way to match this code to your script, post the script here and we'll try to help you.
EDITED: From your script, I suppose you want to "bank" your model based on a pivot 5 units above the center. The easiest way is to do that is to have an intermediate empty object (let's call it Pivot) with local position = (0,5,0). The model is childed to the Pivot, and the Pivot is childed to your character, like this:
Character Pivot ModelWhen banking to any side, just set the Pivot localEulerAngles like this:
Pivot.localEulerAngles = Vector3(0, 0, angle);It's better to set the localEulerAngles as a whole, instead of just setting its z component - setting individual components causes small drifts that may accumulate and let the object in a weird position after some time.
All Rotate functions (Rotate, RotateAround) have the same problem: you loose the angle information, because you just can't trust in the returned euler angles! The 359/-1 uncertainty is just the tip of the iceberg: after some rotations, the craziest angle combinations may be returned, and you have no chance to correctly interpret them.
For this reason, always avoid reading euler angles; if you must rotate something and still know the euler angle at any time, do the inverse thing: keep the angle in a variable, increment or decrement it (always modulo 360) and set the euler angles at once using a Vector3 structure - like in this generic Rotate function:
var xAngle: float = 0; var yAngle: float = 0; var zAngle: float = 0;
function RotatePrecisely(angX: float, angY: float, angZ: float){ xAngle = (xAngle + angX) % 360; yAngle = (yAngle + angY) % 360; zAngle = (zAngle + angZ) % 360; transform.eulerAngles = Vector3(xAngle, yAngle, zAngle); } The precise x, y and z angles are available at the variables xAngle, yAngle and zAngle.
Thanks! I used this to manage tilting the camera location in response to user input.
Just so we're on the same page, is this a way to reset the camera to a specific rotation on button press after rotating the camera around the player? I've been trying to figure that out and made my own question for this issue.
Your answer
Follow this Question
Related Questions
Rotate around moves object out of position, why? 1 Answer
Rotating a camera around a sphere to look at a specific point 2 Answers
How to limit the angle of a camera with transform.RotateAround? 1 Answer
Get the rotation of a object around a arbitrary axis. 0 Answers
Rotate Around Planet with Spaceship Face Set on Path 1 Answer