簡體   English   中英

如何將相機夾在鼠標 Y 軸上?

[英]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;
    }
}

例如,將音高限制為 -40 和 40。我應該以某種方式使用 Mathf.Clamp 嗎?

我認為 Clamp Angle 在這里更適合您,我有一些代碼。

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);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM