Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What's the best strategy for mouse drag to rotate camera with cinemachine Free look camera?

Discussion in 'Cinemachine' started by grahammccarthy, Jan 24, 2020.

  1. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    Hello,
    I've seen a lot of scripts for rotating a generic camera around a character with mouse click and dragging actions, but wondering what is the best strategy in doing this using a Cinemachine Free Look camera?
     
    AbhishekPardhi likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The best way depends on what exactly you're trying to do. Can you give some details about the context and how you want the camera to behave?
     
  3. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    Sure! thanks for the help!

    Here is a link to a video of camera issue: https://drive.google.com/file/d/1eA4_0WNJvXWjqexdTpaHrIHUK8FVNYQG/view?usp=sharing

    You can see I have a cinemachine Free Look camera pointed towards my character.

    I use mouse clicks to move the player, and right now as you move the mouse around the screen, the camera rotates around the player.

    I want the camera to only rotate, if I click and drag the mouse on the screen. So i'm looking for the best way to override the base controls that rotate the camera in a cinemachine camera.

    I've also attached a bit of my cinemachine parameters.

    Screen Shot 2020-01-25 at 10.06.39 AM.png

    Hope this helps!
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You need to remove "Mouse X" from the input axis name field, and replace it with the name of another another Input axis, one that you have set up in the input manager to produce nonzero values only when the user is clicking and dragging.
     
    Freetula and grahammccarthy like this.
  5. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    Awesome! Thanks! That's a great idea!
     
    Gregoryl likes this.
  6. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    Thanks Gregory, I tried to implement this, but not sure exactly how to do it. Any tips?
     
    Coulomb1 likes this.
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
  8. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    That code looks great. Thanks Gregory!
     
    Sierota1701 likes this.
  9. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    I've implemented the following code:

    Code (CSharp):
    1. public class LimitRotatingCamera : MonoBehaviour
    2.     {
    3.         public float TouchSensitivity_x = 10f;
    4.         public float TouchSensitivity_y = 10f;
    5.  
    6.         // Use this for initialization
    7.         void Start()
    8.         {
    9.             CinemachineCore.GetInputAxis = this.HandleAxisInputDelegate;
    10.         }
    11.  
    12.         private float HandleAxisInputDelegate(string axisName)
    13.         {
    14.             switch (axisName) {
    15.                 case "Mouse X":
    16.                     if (Input.touchCount > 0) {
    17.                          //Is mobile touch
    18.                         return Input.touches[0].deltaPosition.x / TouchSensitivity_x;
    19.                     } else if (Input.GetMouseButton(0)) {
    20.                         // is mouse click
    21.                         return Input.GetAxis("Mouse X");
    22.                     }
    23.                     break;
    24.                 case "Mouse Y":
    25.                     if (Input.touchCount > 0) {
    26.                         //Is mobile touch
    27.                         return Input.touches[0].deltaPosition.y / TouchSensitivity_y;
    28.                     } else if (Input.GetMouseButton(0)) {
    29.                         // is mouse click
    30.                         return Input.GetAxis(axisName);
    31.                     }
    32.                     break;
    33.                 default:
    34.                     Debug.LogError("Input <" + axisName + "> not recognized.", this);
    35.                     break;
    36.             }
    37.  
    38.             return 0f;
    39.         }
    40.     }
    This solves my camera rotation only on mouse drag our touch (for mobile).

    My new issue is when you mouse up after rotating the camera, my player moves to the spot you've moused up at.

    I've tried to implement an OnMouseDrag() function on my player but that does not seem to have any effect. How do I detect if I've just finished rotating my camera, so don't allow my player to move, but if I tap on a spot after rotating my camera, he'll move.

    Here is a snippet of my player move script. I have a navmesh that I raycast a point out and tell my navmesh to move there.

    Code (CSharp):
    1. Vector3 target;
    2.             bool hasHit = RaycastNavMesh(out target);
    3.             if (hasHit)
    4.             {
    5.                 if (Input.GetMouseButtonUp(0) && !WasRotatingCamera())
    6.                 {
    7.                     GetComponent<Mover>().StartMoveAction(target, 1.0f);
    8.                 }
    9.                 return true;
    10.             }
    11.             return false;
    And it's the WasRotatingCamera() function that I am trying to implement.
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Maybe your HandleAxisInputDelegate should be keeping track of things. If the button is down and axis value is nonzero, user is dragging, until mouse button is no longer down. WasRotatingCamera can consider that information.
     
    grahammccarthy likes this.
  11. grahammccarthy

    grahammccarthy

    Joined:
    Feb 14, 2017
    Posts:
    7
    Thanks again Gregory, that worked pretty well!
     
    Gregoryl likes this.