0
votes

I am trying to create the classic third person effect where you right click on the screen and hold to rotate camera around to see. What I'd like to do is make the cursor invisible and anchor the mouse cursor at that point and have the camera rotate through the Mouse X/ Mouse Y axis. I've read that previous posts claim that you can't directly anchor your mouse unless you use

CursorLockedMode.Locked

However I don't want my mouse to jump to the center of the screen unless I was able to return it back to its previous point on the screen afterwards. In these posts I've read the only way to do it is to possibly recreate the cursor to be software controlled and then you can manipulate it's position but I don't know where to begin if that is the case.

            if (rightclicked)
            {
                cursorPosition = currentCursorPosition;
                Cursor.Visible = false;
                //Retrieve MouseX and Mouse Y and rotate camera
            }

Basically, i'm trying to accomplish this in pseudo code and everything i've read makes it seem unattainable.

Thanks in advance.

1
I found something useful for your issue: answers.unity3d.com/questions/698254/… - BlackMB

1 Answers

2
votes

Can you please try this script and see if it answers your question?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraRotateOnRightClick : MonoBehaviour {
    public float angularSpeed = 1.0f;
    public Camera targetCam;
    private Vector3 pivotPointScreen;
    private Vector3 pivotPointWorld;
    private Ray ray;
    private RaycastHit hit;

    // Use this for initialization
    void Start () {
        //Set the targetCam here if not assigned from inspector
        if (targetCam == null)
            targetCam = Camera.main;
    }

    // Update is called once per frame
    void Update () {
        //When the right mouse button is down,
        //set the pivot point around which the camera will rotate
        //Also hide the cursor
        if (Input.GetMouseButtonDown (1)) {
            //Take the mouse position
            pivotPointScreen = Input.mousePosition;
            //Convert mouse position to ray
            // Then raycast using this ray to check if it hits something in the scene
            //**Converting the mousepositin to world position directly will
            //**return constant value of the camera position as the Z value is always 0
            ray = targetCam.ScreenPointToRay(pivotPointScreen);
            if (Physics.Raycast(ray, out hit))
            {
                //If it hits something, then we will be using this as the pivot point
                pivotPointWorld = hit.point;
            }
            //targetCam.transform.LookAt (pivotPointWorld);
            Cursor.visible = false;
        }else if(Input.GetMouseButtonUp(1)){
            Cursor.visible = true;
        }
        if (Input.GetMouseButton (1)) {
            //Rotate the camera X wise
            targetCam.transform.RotateAround(pivotPointWorld,Vector3.up, angularSpeed * Input.GetAxis ("Mouse X"));
            //Rotate the camera Y wise
            targetCam.transform.RotateAround(pivotPointWorld,Vector3.right, angularSpeed * Input.GetAxis ("Mouse Y"));
        }

    }
}