Im trying to make the camera move with the mouse pointer in my game using:
public class camera : MonoBehaviour {
public static Vector3 Point;
public float zDistance;
void FixedUpdate(){
var mousePos = Input.mousePosition;
Point=camera.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,zDistance));
transform.LookAt(Point);
}
}v
It works but the screen moves really fast, too fast to be able to play properly. Is there any way to slow it down?
Vector3 direction = Point - transform.position;
Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);
You can set the speed variable to how slow or fast you want the rotation to be. You can look at the result here (although there the y axis of the object is pointing towards a point).
Everyone else is already correctly mentioning how to modify transform.rotation, but personally I find Quaternions to be really confusing so my preferred method for rotating is by directly modifying transform.forward
You just calculate the direction vector with (targetPosition - transform.position).normalized