- Home /
rotate game object with eurle angles ,
im trying to rotate my game object with eurle angles and get more tha 1 position using only one getkey for my video game
if (Input.GetKey(KeyCode.RightArrow)) {
GetComponent<Transform>().eulerAngles = new Vector3(0, 110, 0);
FindObjectOfType<audioManager>().play("slide");
}
im try to rotate this other positions (0,250,0)
(0,360,0)
(0,630,0)
using only one Input.GetKey(KeyCode.RightArrow)),im triying to use euler angles to rotate my game object using only one getkey and try to get more than one position in the same getkey
if (Input.GetKey(KeyCode.RightArrow)) {
GetComponent<Transform>().eulerAngles = new Vector3(0, 110, 0);
FindObjectOfType<audioManager>().play("slide");
}
Answer by SoshJam · Feb 27, 2021 at 12:06 AM
First of all, replace GetComponent<Transform>()
with transform
. The transform
variable is public, and automatically assigned to all GameObjects, so manually retrieving it every time will just hurt performance and give you a headache.
If you use your AudioManager
multiple times in this script, you should also assign it to a variable at the beginning, so that you don't have to search for it again every time you want to use it.
Now to solve your problem: Use if statements to check which position it should be rotating to. I assume you have a variable that you would use to check which position you need to rotate to next.
if (nextPosition == 1)
{
transform.eulerAngles = new Vector3(0, 110, 0);
}
else if (nextPosition == 2)
{
transform.eulerAngles = new Vector3(0, 250, 0);
}
// Continue this If/Else block for all positions.
Alternatively, if your positions were all equal rotations apart (like 90, 180, 270, 360) you could simply call transform.Rotate(0,90,0);
instead of needing a whole variable.
Your answer
Follow this Question
Related Questions
Euler angle problems with rotation limit script 3 Answers
Get local euler angles by Quaternion 1 Answer
Getting the velocity direction as a euler angle. 2 Answers
,How to clamp my turret rotation? 1 Answer
Why is rotation locking to an axis? 3 Answers