|
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: 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 = 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
(comments are locked)
|
|
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
Model
When 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.
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. I've edit the problem.
Sep 25 '11 at 05:24 PM
jonathan.sant
thx this was awesome
Oct 02 '11 at 01:50 PM
jonathan.sant
(comments are locked)
|

I edited my answer to include a possible solution for what you're trying to do - take a look at that.