1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/CameraControl.cs

43 lines
1.3 KiB
C#
Raw Normal View History

2018-04-21 20:19:06 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
[SerializeField]
2018-04-23 15:12:49 +02:00
private float followSpeed = 0.05f;
[SerializeField]
private float minZoom = 5;
[SerializeField]
private float zoomFactor = 4;
[SerializeField]
private float zoomSpeed = 0.05f;
[SerializeField]
private int startingDist = 5;
[SerializeField]
private GameObject followThis;
2018-04-21 20:19:06 +02:00
void Start() {
2018-04-23 15:12:49 +02:00
Camera.main.orthographicSize = minZoom;
if ( followThis == null )
return;
}
2018-04-21 20:19:06 +02:00
void LateUpdate() {
if ( followThis == null )
return;
2018-04-23 14:12:06 +02:00
var target = followThis.transform.position;
var targetVec = target - transform.position;
2018-04-23 15:12:49 +02:00
targetVec.Scale(new Vector3(followSpeed, followSpeed, 0));
transform.position = transform.position + targetVec;
2018-04-23 15:12:49 +02:00
Camera.main.orthographicSize = Camera.main.orthographicSize * (1-zoomSpeed) + (minZoom + zoomFactor / 6f * (followThis.GetComponent<Rigidbody2D>().velocity.magnitude)) * zoomSpeed;
}
public void SetFollow(GameObject g) {
followThis = g;
2018-04-23 14:12:06 +02:00
var diff = (transform.position - followThis.transform.position);
diff.Scale(new Vector3(1f, 1f, 0f));
2018-04-23 15:12:49 +02:00
transform.position = transform.position - diff + (Vector3) (Random.insideUnitCircle) * Random.value * startingDist;
}
2018-04-21 20:19:06 +02:00
}