Modification start date
[BattleCats.git] / Assets / Scripts / CameraScripts / DynamicCamera.cs
blob498223ea6dd4184b716d057b5c491e6eca0abc8d
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
5 public class DynamicCamera : MonoBehaviour
8 private GameObject yarn_object;
9 private Rigidbody2D yarn_rigidbody;
10 private Camera this_camera;
12 public Vector2 origonal_position;
13 private float origonal_zoom;
15 public float zoom_to_velocity_ratio;
16 // public float zoom_velocity_activation_threshold;
17 public float max_zoom_level;
18 public float zoom_growth_decay;
19 public float zoom_unzoom_steps;
21 public Vector2 pan_to_velocity_ratio;
22 public Vector2 max_pan_level;
23 public Vector2 pan_snap;
24 public Vector2 min_activation_vel;
26 private Vector2 previous_velocity_zoom;
27 private Vector2 previous_velocity_pan;
29 public Vector2 cam_trail_velocity;
30 private Vector2 cam_location_to_reach;
32 // Use this for initialization
33 void Start()
35 yarn_object = GameObject.FindGameObjectWithTag("YarnPhysics") as GameObject;
36 yarn_rigidbody = yarn_object.GetComponent<Rigidbody2D>() as Rigidbody2D;
37 this_camera = this.GetComponent<Camera>();
38 origonal_position = new Vector2(
39 this.gameObject.transform.localPosition.x,
40 this.gameObject.transform.localPosition.y);
41 origonal_zoom = this_camera.orthographicSize;
44 // Update is called once per frame
45 void FixedUpdate()
47 Vector3 yarn_vel = yarn_rigidbody.velocity;
48 //zoom
49 if (this_camera.orthographicSize + zoom_unzoom_steps <= origonal_zoom + yarn_vel.magnitude * zoom_to_velocity_ratio)
51 this_camera.orthographicSize = origonal_zoom + yarn_vel.magnitude * zoom_to_velocity_ratio;
52 if (this_camera.orthographicSize > max_zoom_level)
54 this_camera.orthographicSize = max_zoom_level;
57 else if (this_camera.orthographicSize - zoom_unzoom_steps > origonal_zoom + yarn_vel.magnitude * zoom_to_velocity_ratio)
59 if (this_camera.orthographicSize > origonal_zoom) {
60 this_camera.orthographicSize -= zoom_growth_decay;
64 //Panx
65 if (Mathf.Abs(yarn_vel.x) > min_activation_vel.x)
67 float vel_x_pan = origonal_position.x + yarn_vel.x * pan_to_velocity_ratio.x;
68 if (Mathf.Abs(vel_x_pan) < Mathf.Abs(max_pan_level.x + origonal_position.x))
70 cam_location_to_reach = new Vector3(
71 vel_x_pan,
72 origonal_position.y);
74 else
76 cam_location_to_reach = new Vector3(
77 max_pan_level.x * Mathf.Sign(yarn_vel.x) + origonal_position.x,
78 origonal_position.y);
81 else
83 cam_location_to_reach = new Vector3(
84 origonal_position.x,
85 origonal_position.y);
87 //Pany
88 if (Mathf.Abs(yarn_vel.y) > min_activation_vel.y)
90 float vel_y_pan = origonal_position.y + yarn_vel.y * pan_to_velocity_ratio.y;
91 if (Mathf.Abs(vel_y_pan) < Mathf.Abs(max_pan_level.y + origonal_position.y))
93 cam_location_to_reach = new Vector3(
94 cam_location_to_reach.x,
95 vel_y_pan);
97 else
99 cam_location_to_reach = new Vector3(
100 cam_location_to_reach.x,
101 max_pan_level.y * Mathf.Sign(yarn_vel.y) + origonal_position.y);
104 else
106 cam_location_to_reach = new Vector3(
107 cam_location_to_reach.x,
108 origonal_position.y);
111 //grow to the pan
113 GameObject.FindGameObjectWithTag("CameraDestination").transform.localPosition = cam_location_to_reach;
115 float x_dest = transform.localPosition.x + Mathf.Sign(cam_location_to_reach.x - transform.localPosition.x) * cam_trail_velocity.x;
116 // Debug.Log (Mathf.Abs (x_dest - cam_location_to_reach.x));
117 if (Mathf.Abs(x_dest - cam_location_to_reach.x) > pan_snap.x)
119 transform.localPosition = new Vector2(
120 x_dest,
121 transform.localPosition.y);
123 else
125 transform.localPosition = new Vector2(
126 cam_location_to_reach.x,
127 transform.localPosition.y);
130 float y_dest = transform.localPosition.y + Mathf.Sign(cam_location_to_reach.y - transform.localPosition.y) * cam_trail_velocity.y;
131 // Debug.Log (Mathf.Abs (y_dest - cam_location_to_reach.y));
132 if (Mathf.Abs(y_dest - cam_location_to_reach.y) > pan_snap.y)
134 transform.localPosition = new Vector2(
135 transform.localPosition.x,
136 y_dest);
138 else
140 transform.localPosition = new Vector2(
141 transform.localPosition.x,
142 cam_location_to_reach.y);