mathf.min and mathf.max

Hi, I am trying to set a maximum and minimum rotation for an object with the help of the mathf.min and max methods.

Here is what I got:

 if (Input.GetKey("e"))
   {
     myObject.rotation.x = Mathf.Min(myObject.rotation.x, 30);
     myObject.Rotate(new Vector3(myObjectRotationSpeed * Time.deltaTime, 0, 0),            Space.Self);
   }

I can rotate my object succesfully but the maximum and minimum values do not work. Who has experience in this and wants to help me out?

Thanks in advance.

This isn't going to work as expected because the .x .y and .z values of a rotation do not represent angles. They are part of the way Quaternions work, and generally should be left well alone!

What you need to do is manage the rotation angle in your own variable, and use the Min and Max functions on that, and use Quaternion.Euler or Quaternion.AngleAxis to convert that angle to a quaternion each frame.

--- EDIT ---

In addition, you can use Mathf.Clamp to act as a "Max & Min" combined function.

Here's a simple example:

var angle = 0.0;

function Update() {

    angle += Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
    angle = Mathf.Clamp( angle, -20, 20 );
    transform.rotation = Quaternion.Euler( angle, 0, 0 );

}

Saying something like "does not work" doesn't really convey what it is you expect to happen and what is actually happening.

I'm not sure if you are misunderstanding what the functions do because your description is so poor so I will explain: Min and Max do not set a minimum and maximum value respectively, they return the minimum and maximum value of the two numbers passed in.

Mathf.Min(10.0f,20.0f) returns 10.0f, the minimum of the two.

The code you posted will make it so that the maximum value rotation.x can have is 30 because anything above 30 will not be the minimum of the two.

There are other problems here:

  1. Transform.rotation is a Quaternion and does not contain the angles directly (it stores sin(angle) and cos(angle) for all angles multiplied into some compound numbers it's kind of complicated. You likely want Transform.eulerAngles or Transform.localEulerAngles. Note that these represent sequential rotations and by changing the angles individually, you can get some wacky results. You are better off storing a temporary Vector3 for your angles and then re-assigning after you change it.
  2. You are rotating after you set it to be the minimum, so if your myObjectRotationSpeed is positive, then you will go above the minimum value found before after you find it. You should clamp the value after the Rotate.
  3. Angles go around. While a rotation of 40 will not be the minimum of Mathf.Min(40.0f,30.0f), a rotation of -320 will be the lesser of the two.

You are likely looking for Mathf.Clamp which will constrain the value to be within a given range and, because it does both max and min, it will prevent the angle from wrapping around. Again though, you should Clamp after the rotation if you want to constrain the rotated value and unless you intend to act on the Quaternion, you should use transform.eulerAngles (and probably a temporary variable).

Here is the new code:

private Vector3 Angles;
     void Update()
      {
           Angles.x = Mathf.Min(Angles.x, 30);
           Angles.x = Mathf.Max(Angles.x, 80);

           if (Input.GetKey("e"))
           {
                Angles.x = Angles.x + (1 * Time.deltaTime);
                //firePivot.rotation = new Vector3(Angles, Space.Self);
           }
            Debug.Log(Angles.x);
      }