HashDankHog
HashDankHog

Reputation: 69

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

Upvotes: 0

Views: 321

Answers (1)

Srejon Khan
Srejon Khan

Reputation: 448

You're not applying your Vector3 to transform.position. Also, Clamping way is wrong, which can be clamped simply by 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
}

Upvotes: 1

Related Questions