- Home /
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.
Answer by skovacs1 · Nov 18, 2010 at 08:25 PM
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:
- 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.
- 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.
- 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).
Thanks for your good explanation. Sorry for the poor explanation, I will do better next time. Now that I understand that I should store the $$anonymous$$ and max I see what you mean with setting this to the rotation of the obect. I will take your advice and see what I can come up with.
Answer by duck · Nov 18, 2010 at 08:25 PM
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 );
}
Thanks for your reply. I think I understand what you mean. I'll post back with the results as soon as they work.
added example code (and note the use of "$$anonymous$$athf.Clamp")!
Answer by Jorn · Nov 18, 2010 at 09:22 PM
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);
}
Your answer
Follow this Question
Related Questions
How would I find multiple peaks and valleys in an array of values? 1 Answer
Mouse look X axis not working? 0 Answers
Maximum Rotation 1 Answer
how to set a max health and mana 1 Answer
Maximum Rotation 2 Answers