- Home /
rotate to specific coordinate c#
Hello everyone, I have a little problem with a project I'm on
I got a cube around which I want my player to be able to rotate around, facing a specific face by pushing down a key (like face 1 if key 1 is pushed etc) I know the basic of rotate function, but I don't know how to make the rotation end on specific coordinates...
if anyone could just give me that bit of the code, I'll manage the rest on my own thanks in advance!
Answer by RudyTheDev · Dec 29, 2014 at 10:51 PM
using UnityEngine;
public class RotateMe : MonoBehaviour
{
public float rotationSpeed = 5f;
private float currentYAngle = 0f;
private float targetYAngle = 0f;
void Update()
{
if (Input.GetKey(KeyCode.Alpha1))
targetYAngle = 0 - 15;
else if (Input.GetKey(KeyCode.Alpha2))
targetYAngle = 90 - 15;
else if (Input.GetKey(KeyCode.Alpha3))
targetYAngle = 180 - 15;
else if (Input.GetKey(KeyCode.Alpha4))
targetYAngle = 270 - 15;
currentYAngle = Mathf.LerpAngle(currentYAngle, targetYAngle, rotationSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3(0f, currentYAngle, 0f);
}
}
Glad it works. Don't forget to mark the answer as accepted if this answers your question, so it does not appear as unanswered in the list.
Your answer
Follow this Question
Related Questions
Rotating without gimbal lock 1 Answer
Converting Y-rotation into X & Z coordinates? 1 Answer
Trouble with Vector3.Angle() and RotateAround() 1 Answer
Multiple Cars not working 1 Answer