What I enhanced with the freelook camera

I added an if statement to rotate the camera only when the right mouse button is clicked and hide the cursor (like in dragon age), and also added a zoom functionality that changes the field of view with the mouse wheel. I also tweaked the freeLookCameraRig settings with a faster move speed and a faster turn speed.

I have been trying to do this same thing but it wouldn’t work correctly or I got an error. Could you share your code?

I used this in the update method of “FreeLookCam.cs” to hide the cursor and rotate the cam.

        if (Input.GetKey(KeyCode.Mouse1))
        {
            HandleRotationMovement();
            Cursor.visible = false;
        }
        else
        {
            Cursor.visible = true;
        }

And in “ThirdPersonUserControl.cs” I capture the mouse position and compare it to the current position if it’s the same then I move the character, else I don’t.

private void FixedUpdate()
    {
        // read inputs
        float h = CrossPlatformInputManager.GetAxis("Horizontal");
        float v = CrossPlatformInputManager.GetAxis("Vertical");
        bool crouch = Input.GetKey(KeyCode.C);

        // calculate move direction to pass to character
        if (m_Cam != null)
        {
            //Capture the mouse position
            if (Input.GetMouseButtonDown(1))
            {
                mouseHoldPosition = Input.mousePosition;
            }
            //on mouse release compare the positions
            else if (Input.GetMouseButtonUp(1) && Input.mousePosition == mouseHoldPosition)
            {
                clicked = true;
                switch (cameraRaycaster.layerHit)
                {
                    case Layer.Walkable:
                        currentClickTarget = cameraRaycaster.hit.point;
                        break;
                    case Layer.Enemy:
                        Debug.Log("Not moving to enemy !");
                        clicked = false;
                        break;
                    default:
                        Debug.LogAssertion("We shouldn't be here");
                        currentClickTarget = transform.position;
                        return;
                }
            }
            else if (h > 0 || v > 0 || crouch)
            {
                clicked = false;
            }
            if (clicked)
            {
                var playerToClickPoint = currentClickTarget - transform.position;
                if (playerToClickPoint.magnitude >= walkMoveStopRadius)
                {
                    m_Move = playerToClickPoint;
                }
                else
                {
                    m_Move = Vector3.zero;
                }
            }
            else
            {
                // calculate camera relative direction to move:
                m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
                m_Move = v * m_CamForward + h * m_Cam.right;
            }
        }
        else
        {
            // we use world-relative directions in the case of no main camera
            m_Move = v * Vector3.forward + h * Vector3.right;
        }
        #if !MOBILE_INPUT
        // walk speed multiplier
        if (Input.GetKey(KeyCode.LeftAlt))
        {
            m_Move *= 0.5f;
        }
        else if (Input.GetKey(KeyCode.LeftShift))
        {
            m_Move *= 2;
        }
        #endif
        // pass all parameters to the character control script
        m_Character.Move(m_Move, crouch, m_Jump);
        m_Jump = false;
    }

I think that should do it, give it a shot and then tell me if it didn’t work and what’s the problem.
Cheers.

1 Like

Privacy & Terms