Stop MouseLook moving on camera click?

Hi I was hoping someone can tell me how to edit this script so the camera doesn’t jump around based on where you click. So when whenever I click the camera jumps to the point then I can drag around. I rather have it I drag where the camera was left and move it around this way. Please help.

using UnityEngine;
using System.Collections;

public class CockpitCam : AircraftCamera
{
    public string ZoomInputAxis = "";
    public float LookXSpeed = 10.0f;
    public float LookYSpeed = -10.0f;
    public float ZoomSpeed = 100.0f;
   
    public float MinFOV = 1.0f;
    public float MaxFOV = 179.0f;
   
    private Quaternion startOrientation;
    private Vector3 lastMousePosition;
    private float yRotation;
    private float xRotation;
   
    // Use this for initialization
    public void Start ()
    {
        gameObject.GetComponent<Camera>().enabled = false;
       
        startOrientation = gameObject.transform.localRotation;
        lastMousePosition = ControlFreak2.CF2Input.mousePosition;
        yRotation = 0.0f;
        xRotation = 0.0f;
    }
   
    // Update is called once per frame
    public void Update ()
    {
        if ( CameraActive )
        {
            //Look around.
            if ( ControlFreak2.CF2Input.GetMouseButton(0) )
            {
                Vector3 mouseDelta = ControlFreak2.CF2Input.mousePosition - lastMousePosition;
                yRotation += mouseDelta.x * LookXSpeed * Time.deltaTime;
                xRotation += mouseDelta.y * LookYSpeed * Time.deltaTime;
               
                gameObject.transform.localRotation = startOrientation;
                gameObject.transform.Rotate( transform.parent.right, xRotation, Space.World );
                gameObject.transform.Rotate( transform.parent.up, yRotation, Space.World);   
            }
           
            //Zoom.
            if ( ZoomInputAxis != "" )
            {
                gameObject.GetComponent<Camera>().fieldOfView += -ControlFreak2.CF2Input.GetAxis(ZoomInputAxis) * ZoomSpeed * Time.deltaTime;
            }
            gameObject.GetComponent<Camera>().fieldOfView = Mathf.Clamp( gameObject.GetComponent<Camera>().fieldOfView, MinFOV, MaxFOV );
            lastMousePosition = ControlFreak2.CF2Input.mousePosition;
           
            //Apply to main camera.
            Camera.main.transform.position = transform.position;
            Camera.main.transform.rotation = transform.rotation;
           
            Camera.main.fieldOfView = gameObject.GetComponent<Camera>().fieldOfView;
            Camera.main.nearClipPlane = gameObject.GetComponent<Camera>().nearClipPlane;
            Camera.main.farClipPlane = gameObject.GetComponent<Camera>().farClipPlane;
        }
    }
   
}

You’re asking that a click is ignored… maybe you can insert an “&& (Input.GetAxis(“Mouse X”) != 0 || Input.GetAxis(“Mouse Y”) != 0)”
May have to adjust for a tiny value (other than 0) if for any reason that doesn’t work.
Or, in your code, if you move the mouseDelta calculation above the GetMouseButton… you could compare that to some absolute value that’s zero or small.

1 Like

what you need to do is also track the frame of when the mouse was pressed.
On mouse down set the lastMousePosition equal to the current mouse position (so that the mouse delta is zero that frame).
put that right before the GetMouseButton and it should behave closer to what you want.

1 Like