How do I get an instantiated object to rotate based on the mouse position?

I want an object that spawns when I click to be rotated based on where the mouse is.

Thanks :l

		spawnBall = new Vector3(transform.position.x + 2, transform.position.y, transform.position.z);
		if(shot >= 0) {
			if(Input.GetMouseButton(0)) {
				RaycastHit hit = new RaycastHit();
				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
				if(Physics.Raycast(ray, out hit, 100)) {
					shoot = hit.point;
			
		}
					
						Instantiate (ball, spawnBall, ball.transform.rotation);
						shot = 1;
			}
					

		}
				GameObject[] balls = GameObject.FindGameObjectsWithTag("ball");
					foreach(GameObject sphere in balls) {
						sphere.transform.position = Vector3.MoveTowards(sphere.transform.position, shoot, 10 * Time.deltaTime);		
				}
	}
float tempx = gameObject.transform.position.x - currenttarget.transform.position.x;
			float tempy = gameObject.transform.position.y - currenttarget.transform.position.y;
			float newz = (Mathf.Rad2Deg*Mathf.Atan(tempy/tempx));
			gameObject.transform.rotation = Quaternion.AngleAxis(newz,Vector3.forward);

Well because your cursor does not have a position in the 3d world i will assume you are working in 2d… this is the way i do it.

tempx holds the difference between the object and what i want it to look at (currenttarget) on the x axis
tempy holds the difference “”“”" on the y axis

Mathf.Atan(tempy/tempx)

is a simple trig function to give me the angle to it

Mathf.Rad2Deg*

Is because unity does that in radians so i have to change it to degrees for the rotation

Quaternion.AngleAxis(newz,Vector3.forward)

vector3.forward is setting the axis i want it to rotate on (z axis for 2d games)

you should be able to adapt this code easily for your purposes using

Vector3 mouseCoords = maincam.camera.ScreenPointToRay(Input.mousePosition).origin;

to get the coords of your mouse so you could change currenttarget.transform.position.x to mouseCoords.x and same with y

If you are in a 3d world you need to use google better… look up .lookat