簡體   English   中英

如果速度為 0 或更低,如何鎖定相機水平旋轉?

[英]How do I lock the camera horizontal rotation if speed is 0 or less?

我正在做一個汽車游戲,如果模型速度為空,我試圖阻止相機水平移動,我嘗試了很多東西,但是我似乎無法做到這一點,因為當我讓相機被鎖定時即使物體移動它也保持鎖定狀態,如果不是,即使物體停止它也會繼續移動,所以我想知道我怎樣才能做到這一點。

凸輪類代碼

public class Cam : MonoBehaviour
{
    public float sensitivityHor = 9.0f;
    public float sensitivityVert = 9.0f;
    public float minimumVert = -45.0f;
    public float maximumVert = 45.0f;

    public enum rotationAxes
    {
        //Given aliases to X and Y cordinates
        keyX = 1,
    }
    public rotationAxes axes = rotationAxes.keyX;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        Movement mov = GetComponent<Movement>();
        if (mov.speed > mov.speed && mov.speed > 0 && axes == rotationAxes.keyX)
        {
                //Movimiento en el eje X de la camara
                transform.Rotate(0, Input.GetAxis("Horizontal") * sensitivityHor, 0);
        }

    }

}

運動類代碼

public class Movement : MonoBehaviour
{
    private CharacterController _charController;
    public float speed = 3.0f;
    // Start is called before the first frame update
    void Start()
    {
        _charController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {

            float deltaZ = Input.GetAxis("Vertical") * speed;
            Vector3 movement = new Vector3(0, 0, deltaZ);
            movement = Vector3.ClampMagnitude(movement, speed);
            movement *= Time.deltaTime;
            movement = transform.TransformDirection(movement);
            _charController.Move(movement);

    }

}

你永遠不會改變speed的值,那么檢查它有什么意義呢?

而是存儲您將應用於此幀中對象的實際速度

public class Movement : MonoBehaviour
{
    [Header("References")]
    [SerializeField] private CharacterController _charController;

    [Header("Settings")]
    [SerializeField] private float speed = 3.0f;

    [Header("Debug")]
    [SerializeField] private float actualSpeed;

    // public Read-only property
    public float ActualSpeed => actualSpeed;

    // Start is called before the first frame update
    private void Awake()
    {
        if(!_charController) _charController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    private void Update()
    {
        actualSpeed = Input.GetAxis("Vertical") * speed;
        // It is cheaper to clamp a single float value then a vector
        // since "GetAxis" retusn a value between -1 and 1
        // it will anyway never be greater then "speed" so enough
        // to clamp it downwards
        actualSpeed = Mathf.Max(deltaZ, 0);
        actualSpeed *= Time.deltaTime;

        // This already uses the world space forward vector
        _charController.Move(transfor.forward * actualSpeed);
    }
}

現在你可以檢查這個actualSpeed

[RequireComponent(typeof(Movement))]
public class Cam : MonoBehaviour
{
    [Header("References")]
    // Better reference this already via the Inspector
    [SerializeField] private Movement mov;

    [Header("Settings")]
    [SerializeField] private float sensitivityHor = 9.0f;
    [SerializeField] private float sensitivityVert = 9.0f;
    [SerializeField] private float minimumVert = -45.0f;
    [SerializeField] private float maximumVert = 45.0f;
    [Space]
    [SerializeField] private rotationAxes axes = rotationAxes.keyX;

    public enum rotationAxes
    {
        //Given aliases to X and Y cordinates
        keyX = 1,
    }

    private void Awake()
    {
        // as fallback get it ONCE
        if(!mov) mov = GetComponent<Movement>();
    }

    // Update is called once per frame
    private  void Update()
    {
        if (mov.ActualSpeed > 0 && axes == rotationAxes.keyX)
        {
            transform.Rotate(0, Input.GetAxis("Horizontal") * sensitivityHor, 0);
        }
    }
}

暫無
暫無

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

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