Rotor script (rotation into the - values)
so, i got this script and want to make a object rotate around the z axis till a certain point is reached. the script does that perfectly well, but now i want to make it able to rotate into the -degrees, how could i do that?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Rotor : MonoBehaviour {
public float MaxAngle;
public float MinAngle;
public float speed;
public KeyCode upKey;
public KeyCode downKey;
GameObject ObjectToRotate;
// Use this for initialization
void Start () {
ObjectToRotate = transform.GetChild(0).gameObject;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(upKey) && MaxAngle > transform.GetChild(0).eulerAngles.z && MaxAngle > 0)
{
transform.GetChild(0).Rotate(Vector3.forward);
}
if (Input.GetKey(downKey) && MinAngle < transform.GetChild(0).eulerAngles.z && MinAngle > 0)
{
transform.GetChild(0).Rotate(Vector3.forward);
}
}
}
Answer by brett_ridel · May 09, 2017 at 05:14 PM
According to the documentation (https://docs.unity3d.com/ScriptReference/Transform.Rotate.html), the Vector3.forward parameter is not the axis but the euler angle.
public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);
I say it because I'm not sure this is what you want to do. If so, you can just invert the Euler angle : Rotate(-Vector3.forward)
You can also use the Transform.Rotate version that allows you to specify both the axis and the angle
public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);
With this version, you just have to set the axis as Vector3.forward, and put a positive or negative value for the angle.
the problem is, that if the rotation goes over 180 degrees, it switches to $$anonymous$$us which messes up the limitations.
$$anonymous$$aybe you should think differently the conditions when you want to rotate. What exactly do you want to do ? Do you want to rotate in one way when the up key is pressed, and rotate in the other way when the key down is pressed ? If so, what is the $$anonymous$$axAngle and $$anonymous$$inAngle use for ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612121641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
when trying to rotate, the object transforming itself 0 Answers
Rotating an object with VR controller 1 Answer
Move Single Bone With Script? 0 Answers