简体   繁体   English

不能统一移动相机

[英]cant move camera in unity

I am trying to make it so that when you scroll the camera 'zooms' by moving up or down for one of my projects.我正在努力做到这一点,以便当您通过向上或向下移动我的一个项目来滚动相机“缩放”时。 but whenever I run it I get no errors but I cant move it at all.但是每当我运行它时,我都没有收到任何错误,但我根本无法移动它。 Here is my code.这是我的代码。

using UnityEngine;

public class CameraMovement : MonoBehaviour
{
    public float MaxZoom;
    public float MinZoom;
    public float ZoomSpeed;

    Vector3 p = new Vector3(0,0,0);
    
    // Start is called before the first frame update
    void Start()
    {
        Vector3 p = transform.position;
        p.y = MaxZoom;
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log(Input.mouseScrollDelta.y);
        p.y += Input.mouseScrollDelta.y * ZoomSpeed;

        if (p.y > MinZoom)
        {
            p.y = MinZoom;
        }
        if (p.y < MaxZoom)
        {
            p.y = MaxZoom;
        }
    }
}

You're not applying your Vector3 to transform.position .您没有将 Vector3 应用于transform.position Also, Clamping way is wrong, which can be clamped simply by Mathf.Clamp()另外,Clamping 方式不对,可以通过Mathf.Clamp()

void Update()
{
    Debug.Log(Input.mouseScrollDelta.y);
    p.y += Input.mouseScrollDelta.y * ZoomSpeed;

    p.y = Mathf.Clamp(p.y, MinZoom, MaxZoom); // clamp

    transform.position = p; // apply new position to transform
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM