- Home /
Camera Rotate to mouse position
Hi guys, I need some help to make this code work, I'm making a point and click game ad I want that in some cases the camera act something like an human head (first person view) I want the camera to rotate where the mouse is looking at making it at the center of the screen, so far I've made this code:
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit, 1200)) {
         Debug.DrawLine (ray.origin, hit.point);
         camera_.LookAt(hit.point);
     }
The problem with this code is that the camera spins on itself because the mouse is always pointing to a new world position, how can I make the mouse to move smooth to the center?
Thanks for the help
Answer by whydoidoit · Jun 23, 2012 at 10:16 AM
    var targetRotation : Quaternion;  //Script variable
    var speed = 2.0;
    ...
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 1200)) {
        Debug.DrawLine (ray.origin, hit.point);
         targetRotation = Quaternion.LookRotation(hit.point - camera_.transform.position);
     }
    ...
    function LateUpdate() {
           transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
    }
@whydoidoit this might be a noob question, but what if the ray doesn't hit anything? wouldn't the whole expression evaluate to false and none of the statement would be executed?
Answer by makegoalken · Mar 04, 2020 at 08:16 AM
A very meaningful event, I hope everything will go well temple run
The problem with this code is that the camera spins on itself because the mouse is always pointing to a new world position, how can I make the mouse to move smooth to the center?
I could not solve this problem too, for my project rapidessay, I hope that I will be able to cope with that.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                