SHOW:
|
|
- or go back to the newest paste.
1 | using UnityEngine; | |
2 | using static UnityEditor.PlayerSettings; | |
3 | ||
4 | public class AlienLeaderScript : MonoBehaviour | |
5 | { | |
6 | public GameObject alienBullet; //bullet prefab field | |
7 | public float speed = 10.0f; //movement speed | |
8 | public float borderValue = 8f; //border value on the right and left side | |
9 | //the middle of the screen is located at (0,0,0) so we will have the same value on both sides | |
10 | Vector3 target; //point to which the commander will move | |
11 | Renderer rend; //renderer component | |
12 | ||
13 | private void Start() | |
14 | { | |
15 | //Describing | |
16 | target = new Vector3(borderValue, transform.position.y,transform.position.z); | |
17 | rend = GetComponent<Renderer>(); | |
18 | rend.enabled = false; //switching off the visiblity | |
19 | } | |
20 | ||
21 | //Leader starts | |
22 | public void StartLeader() | |
23 | { | |
24 | Invoke("ToggleVisible", 10); //Call to enable visibility after 10 seconds | |
25 | InvokeRepeating("Shoot", 11, 4); //Call to start firing after 11 seconds and then fire every 4 seconds | |
26 | } | |
27 | ||
28 | ||
29 | //Disabling leader actions | |
30 | public void StopLeader() | |
31 | { | |
32 | CancelInvoke("Shoot"); //Disable shooting | |
33 | Show(); //turn off visibility | |
34 | } | |
35 | ||
36 | ||
37 | //shooting | |
38 | void Shoot() | |
39 | { | |
40 | //calculation of position, in which a missile is about to appear | |
41 | Vector3 pos = transform.position - new Vector3(0, 1, 0); | |
42 | //create a new projectile | |
43 | Instantiate(alienBullet, pos, Quaternion.identity); | |
44 | } | |
45 | ||
46 | //toggling the visibility | |
47 | void Show() | |
48 | { | |
49 | //if the renderer is on, switch it off | |
50 | if(rend.enabled == true) rend.enabled = false; | |
51 | //if not, switch it on | |
52 | else rend.enabled = true; | |
53 | ||
54 | } | |
55 | ||
56 | //Leader movement | |
57 | void Move() | |
58 | { | |
59 | //determine the step by which the character should move | |
60 | var step = speed * Time.deltaTime; | |
61 | //move towards target | |
62 | transform.position = Vector3.MoveTowards(transform.position, target, step); | |
63 | //If the distance between the character and the target is close to zero | |
64 | if (Vector3.Distance(transform.position, target) < 0.001f) | |
65 | { | |
66 | //calculate the new target that will be on the opposite side | |
67 | target = new Vector3(target.x * -1, transform.position.y, transform.position.z); | |
68 | } | |
69 | ||
70 | } | |
71 | ||
72 | void Update() | |
73 | { | |
74 | Move(); | |
75 | } | |
76 | } |