-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCannonController.cs
More file actions
118 lines (110 loc) · 2.94 KB
/
CannonController.cs
File metadata and controls
118 lines (110 loc) · 2.94 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using EZCameraShake;
public class CannonController : MonoBehaviour {
public GameObject cannonBall, cannonShoot;
public TextMesh noOfBallText, extraBallsText, ballTextShadow, extraTextShadow;
public int noOfBalls = 5;
public int noOfExtraballs = 0;
public Transform spawnPoint, extraSpawnPoint;
public float force;
public float distance = 10;
// [HideInInspector]
public bool start;
Rigidbody rb, rb1;
AudioSource aS;
public AudioClip outOfAmmo, shoot;
void Start()
{
aS = this.GetComponent<AudioSource>();
}
void Update()
{
if(Input.GetMouseButtonDown(0) && noOfBalls<=0 && start)
{
aS.clip = outOfAmmo;
aS.Play();
}
if(Input.GetMouseButtonDown(0) && noOfBalls>0 && start)
{
// this.GetComponentInChildren<Animator>();
this.GetComponentInChildren<Animator>().Play("Recoil");
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(cannonShoot);
if(noOfExtraballs>0)
{
rb = Instantiate(cannonBall, spawnPoint.position, Quaternion.identity).GetComponent<Rigidbody>();
rb1 = Instantiate(cannonBall, extraSpawnPoint.position, Quaternion.identity).GetComponent<Rigidbody>();
rb.transform.LookAt(mousePos);
rb1.transform.LookAt(mousePos);
rb.AddForce(rb.transform.forward * force*1000);
rb1.AddForce(rb.transform.forward * force*1000);
}
else
{
rb = Instantiate(cannonBall, spawnPoint.position, Quaternion.identity).GetComponent<Rigidbody>();
rb.transform.LookAt(mousePos);
rb.AddForce(rb.transform.forward * force*1000);
}
BallsReduce();
aS.clip = shoot;
aS.Play();
// transform.LookAt(mousePos);
CameraShaker.Instance.ShakeOnce(1.5f,1.5f, 0f, 0.5f);
}
TextUpdate();
}
void BallsReduce()
{
noOfBalls --;
if(noOfExtraballs>0)
{
noOfExtraballs --;
}
}
public void AddBalls(int add)
{
noOfBalls += add;
}
public void AddExtraBalls(int add)
{
noOfExtraballs += add;
}
void TextUpdate()
{
if(!start)
{
noOfBallText.gameObject.SetActive(false);
ballTextShadow.gameObject.SetActive(false);
}
else
{
noOfBallText.gameObject.SetActive(true);
ballTextShadow.gameObject.SetActive(true);
}
if(noOfBalls >0)
{
noOfBallText.text = "x " + noOfBalls;
}
else
{
noOfBallText.text = "NO BALLS";
}
ballTextShadow.text = noOfBallText.text;
if(noOfExtraballs >0)
{
extraBallsText.gameObject.SetActive(true);
extraTextShadow.gameObject.SetActive(true);
extraBallsText.text = "+ " + noOfExtraballs + " extra";
extraTextShadow.text = extraBallsText.text;
}
else
{
extraBallsText.gameObject.SetActive(false);
extraTextShadow.gameObject.SetActive(false);
}
}
}