Sorry for the late reply, and thanks for the help. When I use the speed at 0.1, it doesn’t rotate at all, when I use the speed of 5 it moves about right. Now I am having an issue with returning the object back to its original position my code looks like this:
Now the speed that I am using is 50 because it doesn’t rotate back to the starting point of 0 if I do not use it. So I am wondering if there is a way to rotate the object smoothly back to 0 using this line of code that I just posted.
You need to set the starting position to a variable at the start of the script and do a check of open or closed. It would probably end up looking something similar to this.
private var startPos:Vector3;
var secondPos:Vector3 = new Vector3(0f, 45f, 0f);
var speed:float = 5;
var openDoor:boolean = false;
function Start()
{
startPos = transform.rotation.eulerAngles;
}
function Update()
{
if(openDoor)
{
transform.rotation = Quaternion.Euler(Vector3.Slerp(transform.rotation.eularAngles, secondPos , speed * Time.deltaTime));
}else{
transform.rotation = Quaternion.Euler(Vector3.Slerp(transform.rotation.eularAngles, startPos , speed * Time.deltaTime));
}
}
You may have issues with this if it goes above 360 or below 0 though. maybe… I can’t confirm, I’m at work, but my though is, if it sees the angle of 372, unity may actually see that value as 12, and go the wrong direction. Don’t know, you’d have to test it out. (Like I said, at work, so I can’t test it)
Don’t store rotations as Euler angles, that’s a lossy format. Try to work as much as possible with Quaternions directly:
Also, Quaternions aren’t positions, they’re just rotations. They should be called Rot, not Pos.
The easiest way to do this is to probably just keep a target rotation, and always lerp to it, like this:
private var tgtRot:Quaternion;
var tgtAngle = 0;
var tgtAxis:Vector3 = new Vector3(0, 1, 0);
var speed:float = 5;
function Start()
{
Open(); // or Close(), whichever you want.
}
public function Open()
{
tgtAngle = 45;
tgtRot = Quaternion.AngleAxis(tgtAngle, tgtAxis);
}
public function Close()
{
tgtAngle = 0;
tgtRot = Quaternion.AngleAxis(tgtAngle, tgtAxis);
}
function Update()
{
transform.rotation = Quaternion.Slerp(transform.rotation, tgtRot, speed * Time.deltaTime);
}
Thanks for the reply’s and sorry I am late on this. I did that about doing the rotation when an event is triggered and when I leave the trigger I want to rotate back to the original position and that worked also hence the code that uses Quaternion.identity but it just snaps back into place so I am trying something to make it smoothly rotate hopefully I get that working soon.
Just to add another point to this I have added the Input key to rotate my object but when it rotates the object depending on the direction it rotates it pushes the object up or down as follows
Now I set the input to negative so when you are pushing the left arrow key the object rotates to the right and then I have it translating to the right but for some reason the object goes down and you have to push the up arrow key in order to get the object to become stable and leveled. If this doesn’t make sense let me know but I am confused on this one.
Thanks for the replies but I got it working, the thing that I didn’t do and forgot to do was set my initial rotation to zero like so
transform.rotation = Quaternion.identity;
The once I set that I setup my from rotation to my to rotation to be the exact same for some reason it leaves my object balanced at the same level that way which was one of my issues in the first place then set my rotation to 1.0 and it smoothed it out.
If anyone else needs help hope this works for ya.