-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainCamera.cs
143 lines (117 loc) · 4.88 KB
/
MainCamera.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using UnityEngine;
using System.Collections;
public class MainCamera : MonoBehaviour
{
//gameobjects
public GameObject lightningPrefab;
public GameObject cloud;
public Texture texCloud;
// public reference objects
public GameObject player;
// Constants
float CAMERA_DISTANCE;
const float PLAYER_SPEED = 80;
const float SCALE_MODIFIER = 1.7F;
const int DIRECTIONRIGHT = 90; //direction = right.
const int DIRECTIONLEFT = 270;
const int CLOUD_HEIGHT = 200;
void Start()
{
CAMERA_DISTANCE = GameObject.Find("Player").transform.position.z;
}
void Update()
{
WorldClick();
Raycast();
}
void WorldClick()
{
// Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began
// Input.GetTouch(0).position
// Input.GetMouseButtonDown(0)
// Input.mousePosition
if (Input.GetMouseButtonDown(0))
{
Debug.Log("hej");
// finds the cursor's position and store it in worldPos.
Vector3 screenPos = Input.mousePosition;
screenPos.z = CAMERA_DISTANCE;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);
worldPos = new Vector3(worldPos.x, worldPos.y, player.transform.position.z);
//Stores the position of where the lightning should strike in InstancePos.
Vector3 instancePos = new Vector3(worldPos.x, cloud.transform.position.y, cloud.transform.position.z);
//Stores the object created in the scene to the variable lightning.
GameObject lightning = (GameObject)GameObject.Instantiate(lightningPrefab, instancePos, Quaternion.identity);
//Calculates the scale of the lightning so it strikes at where you click.
float objectHeight = lightning.renderer.bounds.size.y;
float desiredHeight = Mathf.Abs(cloud.transform.position.y - worldPos.y);
float yScale = desiredHeight / objectHeight;
//Checks the direction of the player and reverse it.
if (worldPos.x > player.transform.position.x)
{
Vector3 rotateRight = player.transform.eulerAngles;
rotateRight.y = DIRECTIONLEFT; //equals right in the game
player.transform.rotation = Quaternion.Euler(new Vector3(
rotateRight.x,
rotateRight.y,
rotateRight.z));
}
else
{
Vector3 rotateLeft = player.transform.eulerAngles;
rotateLeft.y = DIRECTIONRIGHT; //equals right in the game
player.transform.rotation = Quaternion.Euler(new Vector3(
rotateLeft.x,
rotateLeft.y,
rotateLeft.z));
}
// Calculates and apply the actual scaling of the newly created lightning
Vector3 localScale = lightning.transform.localScale / SCALE_MODIFIER;
localScale.z *= -yScale;
lightning.transform.localScale = localScale;
float yOffset = lightning.renderer.bounds.size.z;
Vector3 position = lightning.transform.position;
position.y -= (yOffset);
lightning.transform.position = position;
lightning.transform.rotation = Quaternion.Euler(new Vector3(90, 180, 0));
}
}
void Raycast()
{
// Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began
// Input.GetTouch(0).position
// Input.GetMouseButtonDown(0)
// Input.mousePosition
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
Vector3 mousepos = camera.ScreenToWorldPoint(Input.mousePosition);
if (Physics.Raycast(mousepos, Vector3.forward * 10, out hit))
{
switch (hit.collider.gameObject.name)
{
case "ElevatorTrigger":
hit.collider.gameObject.GetComponent<ElevatorTrigger>().elevator.use = true;
break;
case "BridgeTrigger":
hit.collider.gameObject.GetComponent<BridgeTrigger>().bridge.use = true;
Destroy(hit.collider.gameObject);
break;
case "Destructable":
Destroy(hit.collider.gameObject);
break;
case "Player":
Application.LoadLevel(Application.loadedLevel);
break;
case "FallingBlockTrigger":
hit.collider.gameObject.GetComponent<FallingBlockTrigger>().fallingBlock.use = true;
break;
}
}
}
}
void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, CLOUD_HEIGHT), texCloud);
}
}