Camera Control

mail@pastecode.io avatar
unknown
csharp
22 days ago
930 B
7
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    Transform targetPlayer;
    Vector3 velocityCamera = Vector3.zero;


    [Range(0, 1)]
    [SerializeField] float smoothTime;

    [SerializeField] Vector3 posOffset;

    [Header("Camera Limitation")]
    [SerializeField] Vector2 xCamLimit;
    [SerializeField] Vector2 yCamLimit;

    private void Awake()
    {
        targetPlayer = GameObject.FindGameObjectWithTag("Player").transform;
    }

    private void LateUpdate()
    {
        Vector3 targetPos = targetPlayer.position + posOffset;
        targetPos = new Vector3(Mathf.Clamp(targetPos.x, xCamLimit.x, xCamLimit.y), Mathf.Clamp(targetPos.y, yCamLimit.x, yCamLimit.y), -10);
        transform.position = Vector3.SmoothDamp(transform.position, targetPos, ref velocityCamera, smoothTime);        
    }
}
Leave a Comment