Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

On Mouse Click Camera Rotation

Discussion in 'Scripting' started by KenpachiZarak11, Jan 8, 2016.

  1. KenpachiZarak11

    KenpachiZarak11

    Joined:
    Jan 8, 2016
    Posts:
    2
    Hey there,

    First question here, and I'm new to Unity and the Forums so be nice. I did look around quite a bit to see if my question had been answered, but I couldn't find anything.

    Okay, so I'm using the Mouse Orbit Improved script here: http://wiki.unity3d.com/index.php?title=MouseOrbitImproved and its what I need as far as camera movement goes for circling around a 3d object using the mouse. What I'd like to make happen is to only activate the movement upon mouse click. Or in other words, be able to drag the camera around my object instead of it being constantly moving relative to mouse movement.

    I'm a rookie, but I tried this a couple ways and it either broke unity, or only updated at the exact moment I clicked. Obviously having *Input.GetMouseButtonDown(0)* is used here somewhere, but I can't figure out how to implement it. Thank you so much for your help!

    Code (csharp):
    1.  
    2. void LateUpdate ()
    3.     {
    4.             if (target) {
    5.            
    6.                 //So this is the driving force from Mouse to Camera Movement,
    7.                 //but I want to only call this while the left mouse button is pressed.
    8.  
    9.                 x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
    10.                 y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    11.  
    12.                //
    13.          
    14.                 y = ClampAngle(y, yMinLimit, yMaxLimit);
    15.          
    16.                 Quaternion rotation = Quaternion.Euler(y, x, 0);
    17.          
    18.                 distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
    19.          
    20.                 RaycastHit hit;
    21.                 if (Physics.Linecast (target.position, transform.position, out hit))
    22.                 {
    23.                     distance -=  hit.distance;
    24.                 }
    25.                 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    26.                 Vector3 position = rotation * negDistance + target.position;
    27.          
    28.                 transform.rotation = rotation;
    29.                 transform.position = position;
    30.             }
    31.     }
    32.  
    Edited to include code formatting.
     
    Last edited: Jan 8, 2016
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,

    LateUpdate is called every frame, after the Update function.
    Code (csharp):
    1. Input.GetMouseButtonDown(0)
    is true only for the frame when the button was clicked (er....excuse my broken english)
    You should use GetMouseButton instead, it will return true each frame if the button is clicked.

    a simple fix could be :
    Code (csharp):
    1.  
    2. void LateUpdate ()
    3. {
    4.    if (!Input.GetMouseButton(0))
    5.       return;
    6.  
    7.    if (target)
    8.    {
    9.        //....
    10.    }
    11. }
    12.  
    I hope i was nice : )
     
    KenpachiZarak11 likes this.
  3. KenpachiZarak11

    KenpachiZarak11

    Joined:
    Jan 8, 2016
    Posts:
    2
    Perfect! Did exactly what I was trying to do. Thank you so much!