well this is my door script but every time it runs, sometimes it goes in 360 degrees because it goes past the closing and it was 270. so basically (270 + 90) = 360 (in unity that = 0) so it keeps going
and of course the opposite way, when its open 90 degrees and starts to close, it goes past 0.
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var startPosition = 0;
var open : boolean;
var enter : boolean;
var rotateAround : Transform;
function Start(){
startPosition = transform.eulerAngles.y;
if(transform.eulerAngles.y > 264 transform.eulerAngles.y < 300){
transform.eulerAngles.y = 265;
startPosition = 265;
}
if(transform.eulerAngles.y > 300){
transform.eulerAngles.y = 0;
startPosition = 0;
}
}
//Main function
function Update (){
if(open == true){
//var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
//transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
//Time.deltaTime * smooth);
if(rotateAround.transform.eulerAngles.y < DoorOpenAngle + startPosition){
transform.RotateAround (rotateAround.position, Vector3.up, Time.deltaTime * smooth);
}
}
if(open == false){
// var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
if(transform.eulerAngles.y > 3 + DoorCloseAngle + startPosition){
transform.RotateAround (rotateAround.position, Vector3.up, Time.deltaTime * -smooth);
//transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
// Time.deltaTime * smooth);
}
}
}
//Activate the Main function when player is near the door
function activate(){
if(open){
open = false;
}else{
open = true;
}
}
Use quaternions when interpolating rotations. There are a nice set for methods provided by Unity. If your doorâs rotation when closed is 0,0,0, then you can use Slerp() to Quaternion.Identity, which is the equivalent of eular 0,0,0. Give it some R&D and post for help if you need. There are quite a few posts on the subject for reference.
While Lerp should fix any issues where the result will rotate the wrong way, a vector3 linear interpolation will not give nice results if the door opens wide. I also would not recommend using Vector3s to represent rotations (a look at point sure, but not a rotation) because eular (x,y,z) rotations are horrible at interpolation. In the posted code, Unity will be getting and setting to quaternions, Iâm sure, so the only real problem would be the linear interpolation, and the extra layers of code.
If I wasnât on my iPad I would post some code, sorry. You basically just define the rotations and slurp from Quaternion.Identity to and from.
What you are encountering is an annoying problem called âGimble Lock.â This happens because Unity lerps the numbers along a standard ânumber line.â So going from, say 1° to 359° is a positive change of 347°, not a negative change of 2°, even though the end result would appear to be the same.
As theyâre describing above - the best way to work around this problem is by using Quaternions. Note that you donât need to understand the math behind them in order to understand how to use them. In general, you just need to know that a quaternion rotation represents a given orientation of an object in space and that you can interpolate between sets/pairs of these.
Quaternion.Identity typically corresponds to a EulerAngle Rotation of x=0 y=0 z=0.
Youâll probably have good luck using both Quaternion.identity and Quaternion.LookRotation. Both are explained in the scripting reference.