GeoJam2021/Assets/Scripts/CameraTracking.cs

39 lines
981 B
C#
Raw Normal View History

2021-07-19 11:45:08 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraTracking : MonoBehaviour
{
public Transform player;
2021-07-22 14:52:39 +02:00
[Range(0.1f, 2)] [SerializeField] float smoothing = 0.6f;
2021-07-19 11:45:08 +02:00
// Start is called before the first frame update
2021-07-22 14:52:39 +02:00
Vector3 player_pos;
Vector3 camera_pos;
Vector2 difference = new Vector2(0, 0);
2021-07-19 11:45:08 +02:00
void Start()
{
2021-07-22 14:52:39 +02:00
camera_pos = transform.position;
camera_pos[2] = -10;
transform.position = camera_pos;
2021-07-19 11:45:08 +02:00
}
// Update is called once per frame
void Update()
{
2021-07-22 14:52:39 +02:00
player_pos = player.transform.position;
camera_pos = transform.position;
difference[0] = player_pos[0] - camera_pos[0];
difference[1] = player_pos[1] - camera_pos[1];
float smoothing_ = smoothing / Time.deltaTime;
camera_pos[0] += difference[0] / smoothing_;
camera_pos[1] += difference[1] / smoothing_;
transform.position = camera_pos;
2021-07-19 11:45:08 +02:00
}
}