How do I limit the rotation of 2-axis prefabs in Unity?

2

I need a prefab to turn when it reaches 2 positive values in Y. In case it reaches 65º. My code is like this, it just is not working.

 private float m_MaxYRotation = 65f;    
    private float m_MinYRotation = -65f;  
    private float velocity = 3;

    private void Update()   {
        var eulerRotation = transform.rotation.eulerAngles;

        eulerRotation.y = Camera.main.transform.eulerAngles.y;

        if (eulerRotation.y < 270)
            eulerRotation.y += 360;

       eulerRotation.y = Mathf.Clamp(eulerRotation.y, 180 + m_MinYRotation, 180 + m_MaxYRotation);

        transform.localEulerAngles = (Vector3.down * eulerRotation.y  * velocity);
    
asked by anonymous 22.02.2016 / 19:35

2 answers

1

uses the Mathf.Clamp () function; Example:

Vector3 move;
float valormin = 0;
float valormax = 90;
move.x = Mathf.Clamp(move.x, valormin, valormax);

This example would cause a vector3 to a value of x to be between 0 and 90

    
11.07.2017 / 00:17
0

First you must take the Y-rotation of the object and check the value of the rotation with the established limit, if it does not exceed the maximum value, the object can continue to receive the rotation.

The logic would look like this:

if(transform.eulerAngles.y > 65f){
 debug.log("Passou de 65ª");
}else{
  //Codigo que continua atribuindo a rotação no objeto.
}
    
30.03.2016 / 13:56