How to make an object point at the cursor

How can I maek an object(a flashlight in my case) point at the cursor all the time

A cursor is 2D, so it’s world position depends on whatever distance from the camera you want to use when you translate Screen coordinates to World coordinates. Here is a bit of code that will look at the cursor. Values for ‘factor’ below 1.0 will cause the object look towards the camera. Values greater than 1.0 will cause the object to look away from the camera. Experiment with different values for ‘factor’ to get the angle of rotation you want.

#pragma strict

var factor = 0.7;  
 
function Update() {
	var dist = (transform.position.z - Camera.main.transform.position.z) * factor;
	var pos = Input.mousePosition;
	pos.z = dist;
	pos = Camera.main.ScreenToWorldPoint(pos);
	transform.LookAt(pos);
}

Note if the camera moves or is not axis aligned, you’ll want to change line 6 to:

 var dist = Vector3.Distance(transform.position, Camera.main.transform.position) * factor;