NullReferenceException: Object reference not set to an instance of an object MultipelCamera.Zoom () (at Assets/MultipelCamera.cs:36) MultipelCamera.LateUpdate () (at Assets/MultipelCamera.cs:30)
using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(Camera))] public class MultipelCamera : MonoBehaviour { public List<Transform> targets; public Vector3 offset; public float smoothTime = .5f; public float minZoom = 40f; public float maxZoom = 10f; public float zoomLimiter = 50f; private Camera cam; private Vector3 velocity; void start() { cam = GetComponent<Camera>(); } void LateUpdate() { if (targets.Count == 0) return; move(); Zoom(); } void Zoom() { float newZoom = Mathf.Lerp(maxZoom, minZoom, GetGreatestDistance() / zoomLimiter); cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, newZoom, Time.deltaTime); } float GetGreatestDistance() { var bounds = new Bounds(targets[0].position, Vector3.zero); for (int i = 0; i < targets.Count; i++) { bounds.Encapsulate(targets[i].position); } return bounds.size.x; } void move() { Vector3 centerPoint = GetCenterPoint(); Vector3 newPosition = centerPoint + offset; transform.position = Vector3.SmoothDamp(transform.position, newPosition, ref velocity, smoothTime); } Vector3 GetCenterPoint() { if (targets.Count == 1) { return targets[0].position; } var bounds = new Bounds(targets[0].position, Vector3.zero); for (int i = 0;i < targets.Count; i++) { bounds.Encapsulate(targets[i].position); } return bounds.center; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)