How do I limit the rotation of prefabs on 2 axes in Unity?

Asked

Viewed 268 times

2

I need a Prefab to stop spinning when it reaches 2 positive values in Y. In case it reaches 65º. My code is like this, only it’s 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);

2 answers

1

uses the function Mathf.Clamp(); Example:

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

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

0

First you should take the Y rotation of the object and check the rotation value with the established limit, where if it does not exceed the maximum value established, the object can continue receiving the rotation.

Logic would be like this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.