- Home /
Open and close doors by pressing 'E'
Hey,
I have this structure of code inside an update function:
if (Input.GetKey(KeyCode.E)){
Pressed = true;
}
//rotate
if (Pressed == true)
{
Slerpfunction
//print("End Reached");
Pressed = false;
}
//rotate back
if (Pressed == true)
{
Slerpfunction
//print("End Reached");
Pressed = false;
}
}
I know that this won't work, but how can I make it so that when I press 'e' it rotates, then when the rotation is completed and I press 'e' again, it rotates back, and then when I press 'e' again, it rotates again and so on. Thx!
Answer by $$anonymous$$ · May 15, 2015 at 04:22 PM
Here is your code, I used Quaternion.Lerp() instead of Slerp to make it simpler but you can replace that code with yours.
private bool Pressed;
private int State;
private Quaternion EndRot1, EndRot2;
private void Start()
{
EndRot1 = Quaternion.Euler(0, 90, 0); // Could be also set from the Inspector
EndRot2 = Quaternion.identity;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
Pressed = true;
if (Pressed) // Rotation
{
if (transform.rotation == (State == 0 ? EndRot1 : EndRot2)) // Did the rotation end?
{
State ^= 1; // Inverts "State" from 0 to 1 and reverse
Pressed = false;
} // Slerp Method Here
else this.transform.rotation = Quaternion.Lerp(transform.rotation, State == 0 ? EndRot1 : EndRot2, Time.deltaTime * 5);
}
}
hey I'll take a look at this, haven't got time yet though, thanks for answering!
hey, just a question, what does the ^= operator do?
and the comparison with the question mark means:
if state == 0 --> rotation = Endrot1 ////// if state == 1 --> rotation = Endrot2
If you define INT variable, C# automaticly creates a 'safe code' where it assigns 0 to value types and NULL to reference types automaticly so you dont have to do it but you can if you want.
x ^= 1 operator simply swaps 0 and 1 value, if you have 1, it negates it to 0 and reverse over an over again :)
Answer by Griffo · May 23, 2015 at 02:21 PM
Another way is Vector3.Lerp .. But it's not the correct way .. ;)
#pragma strict
var openTime : float;
var openPosition : Vector3;
private var originalRotation : Vector3;
private var targetRotation : Vector3;
private var originalTime : float;
private var originalOpenTime : float;
private var z : int;
function Update ()
{
if (Input.GetKeyDown ("e"))
{
RotateObject();
}
}
function RotateObject ()
{
z = (z % 2) + 1;
if(z == 1)
{
originalRotation = transform.localRotation.eulerAngles;
targetRotation = transform.localRotation.eulerAngles + openPosition;
originalOpenTime = openTime;
while (openTime > 0.0f)
{
openTime -= Time.deltaTime;
transform.localRotation.eulerAngles = Vector3.Lerp(targetRotation, originalRotation, openTime / originalOpenTime);
yield;
}
openTime = originalOpenTime;
}else{
originalRotation = transform.localRotation.eulerAngles;
targetRotation = transform.localRotation.eulerAngles - openPosition;
originalOpenTime = openTime;
while (openTime > 0.0f)
{
openTime -= Time.deltaTime;
transform.localRotation.eulerAngles = Vector3.Lerp(targetRotation, originalRotation, openTime / originalOpenTime);
yield;
}
openTime = originalOpenTime;
}
}
hey, sorry but I looked first at the other guys solution and it worked, but thank you so much for your effort of even answering this :D
Your answer
Follow this Question
Related Questions
need my object to rotate between specific angles on the x axis 2 Answers
Quaternion Slerp help 1 Answer
Choppy rotation of character around y-axis 1 Answer
Get slerp to work just as LookAt(,Vector3.right) does 1 Answer
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers