- Home /
How to slowly rotate a gameObject from 0 to -90f and vice versa every 5 seconds?
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).
Currently this is the code I'm using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GuaritaRotation : MonoBehaviour
{
public int waitTime;
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(waitTime);
}
}
}
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!
sem-titulo.png
(193.3 kB)
Comment
Answer by Fariborzzn · Jun 11, 2021 at 12:40 AM
Hey @Feliperpv , Take look at Here
In general what you need is lerping the rotation to desire rotation , so replace your code to this :
transform.rotation = Quaternion.Lerp (transform.rotation, new Quaternion.Euler(angle, transform.rotation.y, tramsform.rotation.z), Time.deltaTime * speed);
Note : The speed parametr controll the speed of lerping. Hope this help Best regards Fariborz