Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question The code of the camera doesn't work

Discussion in 'Getting Started' started by L1L_L1L, May 7, 2024.

  1. L1L_L1L

    L1L_L1L

    Joined:
    May 5, 2024
    Posts:
    1
    the camera doesn't work

    this is the code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class CameraController : MonoBehaviour
    {
    public Transform Player;

    private float CameraSize;
    private float Height;
    // Start is called before the first frame update
    void Start()
    {
    CameraSize = Camera.main.orthographicSize;
    Height = CameraSize * 2;
    }


    // Update is called once per frame
    void Update()
    {
    CalculateCameraPosition();
    }

    void CalculateCameraPosition()
    {
    int PlayerCamera = (int)(Player.position.y / Height);
    float CameraHeight = (PlayerCamera * Height) + CameraSize;

    transform.position = new Vector3(transform.position.x, CameraHeight, transform.position.z);
    }
    }
     

    Attached Files: