Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using static UnityEditor.PlayerSettings;
- public class AlienLeaderScript : MonoBehaviour
- {
- public GameObject alienBullet; //bullet prefab field
- public float speed = 10.0f; //movement speed
- public float borderValue = 8f; //border value on the right and left side
- //the middle of the screen is located at (0,0,0) so we will have the same value on both sides
- Vector3 target; //point to which the commander will move
- Renderer rend; //renderer component
- private void Start()
- {
- //Describing
- target = new Vector3(borderValue, transform.position.y,transform.position.z);
- rend = GetComponent<Renderer>();
- rend.enabled = false; //switching off the visiblity
- }
- //Leader starts
- public void StartLeader()
- {
- Invoke("ToggleVisible", 10); //Call to enable visibility after 10 seconds
- InvokeRepeating("Shoot", 11, 4); //Call to start firing after 11 seconds and then fire every 4 seconds
- }
- //Disabling leader actions
- public void StopLeader()
- {
- CancelInvoke("Shoot"); //Disable shooting
- Show(); //turn off visibility
- }
- //shooting
- void Shoot()
- {
- //calculation of position, in which a missile is about to appear
- Vector3 pos = transform.position - new Vector3(0, 1, 0);
- //create a new projectile
- Instantiate(alienBullet, pos, Quaternion.identity);
- }
- //toggling the visibility
- void Show()
- {
- //if the renderer is on, switch it off
- if(rend.enabled == true) rend.enabled = false;
- //if not, switch it on
- else rend.enabled = true;
- }
- //Leader movement
- void Move()
- {
- //determine the step by which the character should move
- var step = speed * Time.deltaTime;
- //move towards target
- transform.position = Vector3.MoveTowards(transform.position, target, step);
- //If the distance between the character and the target is close to zero
- if (Vector3.Distance(transform.position, target) < 0.001f)
- {
- //calculate the new target that will be on the opposite side
- target = new Vector3(target.x * -1, transform.position.y, transform.position.z);
- }
- }
- void Update()
- {
- Move();
- }
- }
Add Comment
Please, Sign In to add comment