1

My script is working almost perfectly, but I just need to clamp this float and I can't seem to figure out how.

A simplified version...

public HingeJoint doorHinge;
public float rotatedoor = 0.0f;  // Limit this value, min 0 max 120

void Update () {

       float h = Input.GetAxis("Mouse X");
       rotatedoor = rotatedoor + h;


        JointSpring doorSpring = Door.spring;
        doorSpring.targetPosition = rotatedoor;
        Door.spring = doorSpring;
}

I tried adding a min and max float value and then using

rotatedoor = Mathf.Clamp(rotatedoor, minRot, maxRot);

but no luck.

Any help is appreciated.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Chris
  • 15
  • 4
  • Try this: `Math.Max(0, Math.Min(120, rotatedoor))` – Aleks Andreev Oct 21 '18 at 16:28
  • 1
    He's using Unity3D and trying to use the [Mathf.Clamp()](https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html) method to enforce the limits. @Chris, what results are you getting when using Mathf.Clamp()? This should actually be the correct way to do it, unless something else is influencing the `rotatedoor` variable's value. – vinicius.ras Oct 21 '18 at 16:32
  • It was instead stopping at 33 and then after trying again it went up a little but wouldn't go back down.. No idea why. The ternary expression has worked. – Chris Oct 21 '18 at 16:51

2 Answers2

2

You got your answer about clamping it but you really don't need to do this.

It looks like you want to set the limit for HingeJoint. This has built-in property to do so with the JointLimits struct and that's what you should use.

Something like below:

public HingeJoint doorHinge;
public float rotatedoor = 0.0f;  // Limit this value, min 0 max 120

void Update()
{
    //Get the current the limit
    JointLimits limits = doorHinge.limits;

    //Set the limit to that copy 
    limits.min = 0;
    limits.max = 120;

    limits.bounciness = 0;
    limits.bounceMinVelocity = 0;

    //Apply the limit since it's a struct
    doorHinge.limits = limits;

    JointSpring doorSpring = doorHinge.spring;
    doorSpring.targetPosition = rotatedoor;
    doorHinge.spring = doorSpring;
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I already had the limits set. I should have explained that this is for a door system like you would find in Amnesia or Layers Of Fear. The door would never go past the limits but the value of the float would, thus making it take longer to close the door (if that makes sense). Although the fix above has worked I now have another problem. For some reason, after the first interaction it takes two clicks to interact again. – Chris Oct 21 '18 at 17:52
  • You didn't mention you already had the limits set in your question but that's how to do it. Assuming you have already done that but had to limit the rotatedoor variable too then the answer above is ok. Sorry I don't understand your second issue, maybe you should create new question with that issue – Programmer Oct 21 '18 at 17:55
1

There are a number of easy ways to do this, so I'll list the ones that I see below.

rotatedoor = Math.Max( 0f, Math.Min( 120f, rotatedoor ) );

Alternatively, you could use a ternary expression:

rotatedoor = (rotatedoor < 0f) ? 0f : (rotatedoor > 120f) ? 120f : value;

Or, you could use Unity3D's Mathf.clamp():

rotatedoor = Mathf.clamp( rotatedoor, 0.0f, 120f );

There are probably other ways of doing this as well. I would be interested in knowing if a more efficient method exists, since all of these techniques require operating in the update logic, which is not the most efficient way to go about things per se. Hope this helps!

Max von Hippel
  • 2,856
  • 3
  • 29
  • 46
  • It's worth noting that I included the `clamp` function in this answer before Chris mentioned that that was not working. Happy to update the answer down the line once more information is provided. – Max von Hippel Oct 21 '18 at 16:33
  • 1
    I'm not sure why Mathf.Clamp wasn't working, before asking I researched it and that seemed like the best way to do it. The ternary expression has worked as intended though. Thanks! – Chris Oct 21 '18 at 16:48