Two Strange MathF Clamp Issues: The Disabling of OnMouseDrag

Hey friends,

.
I’m having a couple of issues that I haven’t seen addressed before. After a couple of hours of research, I figured it’s time to ask the questions myself to figure out what mistakes I am making. I’m very new to unity, so please let me know if I am doing something stupid. Here’s the first conundrum:
.
MathF clamp works if I use the clamp in the script. However, if I set up the floats to use inside of the Unity Editor itself, it clamps my game objects on play and I cannot move them. I know they are still draggable because they become a trigger when I stop dragging them, they just don’t move. This isn’t that big of a deal since I can get it to work with the solution below, but I want to figure out why it does it. I used the clamp code from the Space Shooter tutorial. here’s the code for my script:

using System.Collections;
using UnityEngine;


[System.Serializable]
public class Boundary
{
    public float xMin, xMax, yMin, yMax;
}


public class Merge_1 : MonoBehaviour
{

    public Rigidbody rb;
    Vector2 mousePos;
    public GameObject Merge_2;
    Collider ObjectCollider;
    public Boundary boundary;


    void Start()

    {
        mousePos = new Vector2(transform.position.x, transform.position.y);
        
        rb = GetComponent<Rigidbody>();

        ObjectCollider = GetComponent<Collider>();

        ObjectCollider.isTrigger = false;

    }

   void OnMouseDrag() // Use this to Move Game Objects By Clicking and Dragging Mouse
    {
        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    IEnumerator noTrigger()
    {
        yield return new WaitForSeconds(0.1f);
        ObjectCollider.isTrigger = false;
    }

   void OnMouseUp()
    {
        ObjectCollider.isTrigger = true;
        StartCoroutine(noTrigger());
    }

   void FixedUpdate()

    {
      rb.MovePosition(mousePos); // Moves Object with Mouse Position with RigidBody Component   

      rb.position = new Vector2
        (
            Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
            Mathf.Clamp(rb.position.y, boundary.yMin, boundary.yMax)
        );
    }

     void LateUpdate()
    {

        Vector2 pos = transform.position;
        pos.x = Mathf.Clamp(pos.x, -8, 8);
        pos.y = Mathf.Clamp(pos.y, -4, 4);
        transform.position = pos;
    }


    void OnTriggerEnter(Collider other) // Allows game objects to be merged and create the next unit
    {

        if (other.gameObject.tag == gameObject.tag) // destroys current object
        {
            Destroy(gameObject); 

            if (GetInstanceID() < other.gameObject.GetInstanceID()) //Creates new object
            {
                Instantiate(Resources.Load("Merge_2"), transform.position, transform.rotation);
            }

        }
      
    }

}

Now, the second issue is in the way I am using Mathf.Clamp. When I use the clamp and give the transform positions in the script itself, it works! The objects are draggable, and they don’t go passed the clamped positions. However, SOMETIMES the objects get stuck on the clamped position and I have to drag it multiple times to unstick it.
.
For example, my x.min clamp is set to -8. If I try to drag the object passed -8 on the x axis, it gets stopped. Perfect. I can then drag it again and move it elsewhere. However, when I stop dragging when the object is at -8, SOMETIMES it gets stuck on -8 and I have trouble dragging it again. When this happens, there is no shaking of the object; it just seems that I haven’t even clicked it until multiple attempts have passed.
.
.
Here are some screenshots of what’s happening on the editor’s side of things:



Just some further notes:
.

  1. I have placed the second solution in late update to stop the game objects from clipping passed the clamped coordinates. This works. If I place them in update or fixed update, they continually try to get passed the clamp when I stop dragging with my mouse passed the clamp. If I place the first clamp I mentioned in late update, the objects are draggable, but the clamps don’t work at all.
    .
  2. The floats for the mathf clamp in my first issue and the ones used in my solution (by listing the values in the script) are identical.
    .
  3. To stop the game objects from getting stuck at the clamped position, I attempted to move the game object with an if statement. Pseudo code: If position is -8, move to -7. It moved, but the object would still sometimes get stuck, so I’m not sure what’s causing the issue.
    .
    .
    There you have it. I’m very new to Unity and have done my best to research before asking, but I haven’t found an answer yet on what I could be doing wrong. I’m hoping it will help newcomers like me to understand what’s going on and how to fix these things!
    .
    .
    Thank you eternally for reading this and perhaps even finding a solution! I apologize if I have overlooked a simple fix. Thank you again!

Cheers,

Sam

First of all assuming theres a “bug” in Mathf.Clamp is the absolute last thing you should assume. You should look for the error / bug in your own code first. Actually Mathf.Clamp is exactly defined here:

public static float Clamp(float value, float min, float max)
{
    if (value < min)
        value = min;
    else if (value > max)
        value = max;
    return value;
}

So there’s nothing that can go wrong in Mathf.Clamp unless you pass nonsense to the method.

The description of your issues are very vague and the code you’ve provided are just seemingly unrelated fragments. Since you seem to have dragging code somewhere, chances are high you do something fishy there. Also what have you done already to debug your issue? You said using hardcoded boundary constants works while using a serialized value does not? This makes no sense. Have you tried printing out the position before and after the clamp? Also print out the boundary values at the point where they are used.

When a certain issue happens only “SOMETIMES” it’s a quite clear indicator that you have some sort of race condition / competing code. You need to get better to understand your own code, which part can influence which other part. Always keep in mind Computers don’t make mistakes, humans do. If a computer makes a mistake, it’s broken and is trash. Unless you have a hardware failure in your machine, code will do 100% what it instructs the CPU to do. Of course there are other factors of complicated code which are difficult to understand or to predict (i look at you, multithreading). If you don’t understand what or why something happens you just don’t have the full picture yet.

However we have a much less complete picture of your issue since we only know what you have written in your question.

i will post is as an answer, transform.position isnt hte same as rigidbody.position (i also learned today) override the fixedupdate code

void FixedUpdate()
{
    rb.MovePosition(mousePos); // Moves Object with Mouse Position with RigidBody Component   
    Vector2 pos = rb.position;
    pos.x = Mathf.Clamp(pos.x, -8, 8);
    pos.y = Mathf.Clamp(pos.y, -4, 4);
    rb.position = pos;
}