Hi! I have this object in my game and I want it to rotate every 5 seconds, opening and closing the street. In order to do that, I need it to rotate in the X axis from 0 degrees (closed) to -90 degrees (open), waiting 5 seconds and then rotating again, this time from -90 to 0 degrees, repeating that cycle.
Currently this is the code I’m using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuaritaRotation : MonoBehaviour
{
public int tempoIntervalo;
float angle = 90f;
void Start()
{
StartCoroutine(Coroutine());
}
IEnumerator Coroutine()
{
while (true)
{
if (angle == 90f)
{
angle = -90f;
}
else if(angle == -90f)
{
angle = 90f;
}
transform.Rotate(angle, 0.0f, 0f);
yield return new WaitForSeconds(tempoIntervalo);
}
}
}
It works but the object goes directly from 0 to -90 and I’d like it to slowly rotate from one angle to another. I’m having some troubles discovering how to make that, so any help is appreciated!
You first find the speed you want to rotate by, which is float rotationspeed = 90 / 5 (degrees per second). You then make a int called multiplier (make it global, aka above Start()). This will be set to 1 when we move the bridge up, 0 when it stands still and -1 when it moves the bridge down. Then you want to call in Update(): transform.Rotate(rotationspeed * multiplier * Time.deltaTime, 0, 0); then you edit your IEnumerator to be: while (true) { Set multiplier to -1 (or 1, depends on starting location), wait 5 seconds, set to 0, wait 5 seconds, set to 1, wait 5 second }.
Edit formatted code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuaritaRotation : MonoBehaviour
{
public int tempoIntervalo;
int multiplier = 1; //Will be -1 to lower, 1 to raisen and 0 to stand still
float rotationSpeed = 0f; //The speed we rotate by, set in Start()
void Start()
{
StartCoroutine(SetMultiplier()); //I took the liberty to make a better function name
rotationSpeed = 90 / 5; //90 degrees per 5 seconds
}
private void Update()
{
if(multiplier==0)
{
return;
}
//This can be somewhat inaccurate so we will also set the accurate rotation when we have reached the final position
transform.Rotate(multiplier * rotationSpeed * Time.deltaTime, 0, 0); // multiplier defines direction,
// Time.deltaTime is the time between each frame
}
IEnumerator SetMultiplier()
{
while (true) //depending on starting position you might need to edit the order of these
{
multiplier = 1; //sets the bridge up
yield return new WaitForSeconds(tempoIntervalo); //waiting for it to rotate down
SetAccurateRotation(0); //Make sure its in the right position
multiplier = 0; //let it stand still
yield return new WaitForSeconds(tempoIntervalo); //waiting
multiplier = -1; //Rotate down
yield return new WaitForSeconds(tempoIntervalo); //Wait for rotation to finish
SetAccurateRotation(-90);//Make sure position is accurate
}
}
//We do this to make sure we have the exact rotation.
void SetAccurateRotation(int angle)
{
transform.rotation = Quaternion.Euler(angle, transform.rotation.y, transform.rotation.z);
} //Sets the rotation to be exactly what you want
}