Clamp float var and have it continue

Hi,

what I am trying to achieve, is for when the player steps within the 'theRadius' of the game object tag, "otherMesh" the varaible will increment past its already clamped value of 0.3f.

Collider [] myCollider = Physics.OverlapSphere(transform.position, theRadius);

    foreach (Collider col in myCollider)
    {
        if (col is MeshCollider)
        {
            if (col.collider.gameObject.tag == "thisMesh")
            {
                  myFloatVar = Mathf.Clamp(myFloatVar + 0.080f * Time.deltaTime, 0.0f, 0.3f);
            }
            if (col.collider.gameObject.tag == "otherMesh")
            {
                  myFloatVar = Mathf.Clamp(myFloatVar + 0.080f * Time.deltaTime, 0.3f, 0.45f);             
            }       

        }
//...
    }

But the varaible remains the same value and does not increment.

Essentially, their is "pause period" for the float varaible, because the player will step outside of the radius at some point in time. When the player enters the radius of the "otherMesh" game object tag, I would like the varaible to increment its already clamped value.

Is their a way to reset the clamp, possibly?

Thanks in Advance.

What is it incrementing to? The scheme you have now will not necessarily clamp to 0.3 ie. if myFloatVar is 0.299 and 0.080f*Time.deltaTime evalulates to > 0.001 then myFloatVar will be greater than 0.3.

However I assume you realise this so:

if the statement

myFloatVar = Mathf.Clamp(myFloatVar + 0.080f * Time.deltaTime, 0.0f, 0.3f)

as a replacement for the if{...} statement still increments to greater than 0.3 then the problem must be somewhere else in your code.

Change the spelling of the name of the variable slightly where you have declared it and then look through the errors that that produces to see all the places you have used/written to that variable. I feel it is likely to be somewhere else that you are modifying it.

You appear to have changed the question now from your original post...

Ok what I think is happening now is that you don't know what order the colliders will be in your myCollider array. Thus when you loop through the colliders you may have lets say 50 "otherMeshes" incrementing the value, BUT if the last meshCollider is a "ThisMesh" then the variable myFloatValue will be clamped back to 0.3f.

In fact it this is most likely to be the case (I would imagine) when your player has just entered the area as your player would be at the maximum radius and probably gets detected last and hence will be at the end of the array.

You need to conditionally evaluate the clamp for items tagged "thisMesh" ie. only clamp it when your player first enters the area (if you want that value reset when your player enters the area) otherwise if you just want it to increment when your player is in the area with no reset then remove the line of code looking for objects tagged "thisMesh".