Daniel Lip
Daniel Lip

Reputation: 11329

How can I clamp the camera on the mouse Y Axis?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CamerasControl : MonoBehaviour
{
    public CinemachineFreeLook freeLookCam;
    public float pitch;
    public float speed;

    // Start is called before the first frame update
    void Start()
    {
        // CM FreeLook2
        // Middle Rig > Aim > Tracked Object Offset X > To be able to control the Y value with the mouse !

    }

    // Update is called once per frame
    void Update()
    {
        pitch += speed * Input.GetAxis("Mouse Y");

        freeLookCam.GetRig(1).GetCinemachineComponent<CinemachineComposer>().m_TrackedObjectOffset.y = pitch;
    }
}

For example to limit the pitch by -40 and 40. Should I use Mathf.Clamp somehow ?

Upvotes: 0

Views: 264

Answers (1)

Muhammad Afzaal
Muhammad Afzaal

Reputation: 66

I think Clamp Angle will work for you better here I have some code.

float ClampAngle(float angle, float from, float to)
 {
     // accepts e.g. -80, 80
     if (angle < 0f) angle = 360 + angle;
     if (angle > 180f) return Mathf.Max(angle, 360+from);
     return Mathf.Min(angle, to);
 }

Upvotes: 1

Related Questions