-
Notifications
You must be signed in to change notification settings - Fork 0
/
rayCast.cs
91 lines (83 loc) · 3.53 KB
/
rayCast.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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class rayCast : MonoBehaviour {
float x;
float y;
private Text disp;
private PlayerInitPos playerpos;
// Use this for initialization
void Start () {
playerpos = GameObject.Find("Persistant").GetComponent<PlayerInitPos>();
disp = GameObject.Find("interactText").GetComponent<Text>();
//Finding and setting center point on screen to match crosshair
x = Screen.width/2;
y = Screen.height/2;
//Locking mouse pointer
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
//Initiates ray from middle of camera
Ray ray = Camera.main.ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
//Player tries to interact with object
if (Input.GetButtonUp("Fire1")){
if (Physics.Raycast(ray, out hit, 10)) {
//If object can be picked up
if (hit.collider.tag == "pickup")
{ //If selected slot is empty
if (GameObject.Find("UI-script").GetComponent<inventoryUpdate>().getSelectedSlot())
{
//Get item identification number for insertion in inventory-array
int id = GameObject.Find("Persistant").GetComponent<inventory>().getItemNr(hit.collider.gameObject);
GameObject.Find("UI-script").GetComponent<inventoryUpdate>().addItem(id);
//Removes item from world when picked up successfully
Destroy(hit.transform.gameObject);
}
//Checks inventory for free space
else {
int id = GameObject.Find("Persistant").GetComponent<inventory>().getItemNr(hit.collider.gameObject);
if (GameObject.Find("UI-script").GetComponent<inventoryUpdate>().addItemToSpecificSlot(id)){
Destroy(hit.transform.gameObject);
}
}
}
}
}
//Teleporting up and down from bunker
if (Input.GetButton("Submit")) {
if (Physics.Raycast(ray, out hit, 10)) {
if (hit.collider.tag == "teleportDown") {
//Setting spawn position and loading level
playerpos.setSpawn("bunker");
Application.LoadLevel("bunker");
}
else if (hit.collider.tag == "teleportUp") {
//Setting spawn position and loading level
playerpos.setSpawn("hatch");
Application.LoadLevel("main");
}
}
}
//Displays tooltip when raycast hits object tagged pickup
if (Physics.Raycast(ray, out hit, 10)) {
if (hit.collider.tag == "pickup") {
//If ray hits object that can be picked up
disp.text = "Left click to pick up " + hit.collider.name;
}
else if (hit.collider.tag == "teleportUp") {
disp.text = "Press 'E' to climb up";
}
else if (hit.collider.tag == "teleportDown") {
disp.text = "Press 'E' to climb down";
}
else
{
//Removes tooltip text
disp.text = "";
}
}
}
}