Help Please

I have an object that I want to move it and with click I want to change the rotation of it. I have buttons and if buttons clicked object change color. My problem is when I want to change color object starts moving. How can I fix it.

if (!isMoving)
        {
            if (Input.GetMouseButton(0))
            {
                Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero);

                if (ray.x > ray2.x)
                {
                    transform.Rotate(0, 0, 1f);
                }
                else if (ray.x < ray2.x)
                {
                    transform.Rotate(0, 0, -1f);
                }
                ray2 = ray;
            }
            else if (Input.GetMouseButtonUp(0))
            {
                myRigidBody.velocity = transform.up * Time.deltaTime * speed;
                FindObjectOfType<OkSpawner>().StartOkSpawner();
                isMoving = true;
            }
        }

You should name your thread title more clearly, based on what your trying to accomplish.

And it is very difficult to understand what you want to do, and what is actually happening, and what you have tried so far. Just guessing here, but it sounds like your taking input from the mouse to move your object - with the “GetMouseButtonUp()” method, and you also have UI buttons that are changing the color of your object. If that is correct, then your going to need to change the logic of your else if statement because its ALWAYS looking for the mouse button to be released, and will always change the velocity when the mouse button is released, regardless if it happens on a UI element or not.

One possible solution is to use raycasting from where the user clicks, and when the raycast hits the UI buttons, don’t do the movement logic, and instead only do the movement when the raycast does not hit a UI button. If you use Google to search for something like “Unity raycast mouse click” you will find many answers about how to do that, and you can modify them to meet your needs.

Good luck!

1 Like