Ok, so I am trying to rotate an Equilateral Triangle in Y-Axis.
So basically I want this Triangle to rotate +120 degrees everytime I press the key “W” and -120 degrees every time I press the Key “S”.
Hello @manikiran22 ! Hope this helps you :
using UnityEngine;
public class FixedAngleRotator : MonoBehaviour
{
public float XAngle;
public float YAngle;
public float ZAngle;
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
transform.Rotate(XAngle, YAngle, ZAngle);
if (Input.GetKeyDown(KeyCode.S))
transform.Rotate(-XAngle, -YAngle, -ZAngle);
}
}
@manikiran22 This will do what you want nicely, I think.
using UnityEngine;
using System.Collections;
public class FixedAngleRotator : MonoBehaviour
{
public Vector3 EulerRotation = new Vector3(0f, 120f, 0f);
public float TimeToRotate = .5f;
private bool rotating;
private void Update()
{
if (!rotating)
{
if (Input.GetKeyDown(KeyCode.W))
{
StartCoroutine(doRotate(EulerRotation));
}
else if (Input.GetKeyDown(KeyCode.S))
{
StartCoroutine(doRotate(-EulerRotation));
}
}
}
private IEnumerator doRotate(Vector3 rotation)
{
Quaternion start = transform.rotation;
Quaternion destination = start * Quaternion.Euler(rotation);
float startTime = Time.time;
float percentComplete = 0f;
rotating = true;
while(percentComplete <= 1.0f)
{
percentComplete = (Time.time - startTime) / TimeToRotate;
transform.rotation = Quaternion.Slerp(start, destination, percentComplete);
yield return null;
}
rotating = false;
}
}