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

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?

1 Like

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?

Sure! thanks for the help!

Here is a link to a video of camera issue: gameTestCameraIssues.mov - Google Drive

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.

Hope this helps!

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.

2 Likes

Awesome! Thanks! That’s a great idea!

1 Like

Thanks Gregory, I tried to implement this, but not sure exactly how to do it. Any tips?

1 Like

I’m not an Input system wizard, so I can’t really help you there. If you can’t convince the Input system to do it, then you can intercept CM’s calls to it and do it yourself. Have a look here for an example: How do I make a CinemachineFreeLook orbiting camera that only orbits when the mouse key is down?

That code looks great. Thanks Gregory!

1 Like

I’ve implemented the following code:

public class LimitRotatingCamera : MonoBehaviour
    {
        public float TouchSensitivity_x = 10f;
        public float TouchSensitivity_y = 10f;

        // Use this for initialization
        void Start()
        {
            CinemachineCore.GetInputAxis = this.HandleAxisInputDelegate;
        }

        private float HandleAxisInputDelegate(string axisName)
        {
            switch (axisName) {
                case "Mouse X":
                    if (Input.touchCount > 0) {
                         //Is mobile touch
                        return Input.touches[0].deltaPosition.x / TouchSensitivity_x;
                    } else if (Input.GetMouseButton(0)) {
                        // is mouse click
                        return Input.GetAxis("Mouse X");
                    } 
                    break;
                case "Mouse Y":
                    if (Input.touchCount > 0) {
                        //Is mobile touch
                        return Input.touches[0].deltaPosition.y / TouchSensitivity_y;
                    } else if (Input.GetMouseButton(0)) {
                        // is mouse click
                        return Input.GetAxis(axisName);
                    }
                    break;
                default:
                    Debug.LogError("Input <" + axisName + "> not recognized.", this);
                    break;
            }

            return 0f;
        }
    }

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.

Vector3 target;
            bool hasHit = RaycastNavMesh(out target);
            if (hasHit)
            {
                if (Input.GetMouseButtonUp(0) && !WasRotatingCamera())
                {
                    GetComponent<Mover>().StartMoveAction(target, 1.0f);
                }
                return true;
            }
            return false;

And it’s the WasRotatingCamera() function that I am trying to implement.

1 Like

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.

1 Like

Thanks again Gregory, that worked pretty well!

1 Like