how do i make my globe stop spinning when the player/user clicks to drag the globe to rotate it and then resume spinning when they let go of the mouse button?

so i am making something where there is a globe that the player/user can interact with. here is the script that makes it spin

public class EarthSpinScript : MonoBehaviour
{
    public float speed = 10f;
   
    



    //globe spins


    void Update()
    {
        transform.Rotate(Vector3.up, speed * Time.deltaTime, Space.World);

        

    }
}

it spins by its self and the user can also drag the globe so it rotates in all directions.
what I want to happen is when the user clicks to drag the globe i want the globe to stop spinning so it is easier for the user to rotate the globe, and then when they let go of the mouse button the spinning will resume.

i’ve had a look around on the forums etc. and i just cant get my head around it!

this is the script i use to drag

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotateEarth : MonoBehaviour
{
    Vector3 mPrevPos = Vector3.zero;
    Vector3 mPosDelta = Vector3.zero;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {

            mPosDelta = Input.mousePosition - mPrevPos;
            if (Vector3.Dot(transform.up, Vector3.up) >= 0)
            {

                transform.Rotate(transform.up, -Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
            }

            else
            {

                transform.Rotate(transform.up, Vector3.Dot(mPosDelta, Camera.main.transform.right), Space.World);
            }

            transform.Rotate(Camera.main.transform.right, Vector3.Dot(mPosDelta, Camera.main.transform.up), Space.World);

        }

        mPrevPos = Input.mousePosition;

       

       
     
    }
}

thank you in advance!

So the script you currently have has no control whatsoever. You assign a rotation in Update() and this will execute no matter what. So we need to add some control to your script. This can be simply done using a bool. We encapsulate the transform.Rotate(…) in an if statement using the bool. Like this:

public class EarthSpinScript : MonoBehaviour
{
	public float speed = 10f;
	public bool spinEarth = false;
	
	void Update()
	{
		if(spinEarth)
		{
			transfrom.Rotate(Vector3.up, speed * Time.deltaTime, Space.World);
		}
	}
}

Now this is the most basic way to control the rotation. You can reference the EarthSpinScript and set the value of the bool to true or false. Now this works, but you could also use so called encapsulation. This means you make your variable private (doesn’t have to be) and use methods to get and set the variable. Using this way, you could also add extra parameters like for instance rotation speed:

public class EarthSpinScript : MonoBehaviour
{
	public float speed = 10f;
	private bool spinEarth = false;
	
	void Update()
	{
		if(spinEarth)
		{
			transfrom.Rotate(Vector3.up, speed * Time.deltaTime, Space.World);
		}
	}
	
	public void StopSpinning()
	{
		spinEarth = false;
	}
	
	public void StartSpinning(float rotationSpeed)
	{
		speed = rotationSpeed;
		spinEarth = true;
	}
}

Now because you drag in another script, you have to set this variable from the other script. This can be done like this:

public class EarthDrag : MonoBehaviour
{
	//Assign the object in the inspector.
	public EarthSpinScript earthSpin;
	
	public void MyDraggingFunction()
	{
		//code...
		
		earthSpin.StopSpinning();
		earthSpin.StartSpinning(10f);
		
		//Or when you just use a boolean:
		earthSpin.spinEarth = false;
		earthSpin.spinEarth = true;
		
		//code...
	}
}

Hope this gives you an idea how to handle this. Good luck!