Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- public class SniperEnemy : Enemy
- {
- [SerializeField]
- GameObject bullet;
- [SerializeField]
- GameObject rifleStart;
- [SerializeField]
- float cooldown = 1f;
- float timer = 0;
- [SerializeField]float area = 25;
- private enum SniperState
- {
- Wait,
- Prepare,
- Shoot
- }
- private SniperState _currentState = SniperState.Wait;
- void Start() { }
- public override void Move()
- {
- if (_currentState == SniperState.Wait)
- {
- GetComponent<CharacterController>().Move(transform.forward * Time.deltaTime * 2f);
- timer += Time.deltaTime;
- if (timer > 10)
- {
- transform.Rotate(new Vector3(0, 90, 0));
- timer = 0;
- }
- }
- }
- public override void Attack()
- {
- switch (_currentState)
- {
- case SniperState.Wait:
- if (Vector3.Distance(transform.position, player.transform.position) <= area)
- {
- _currentState = SniperState.Prepare;
- timer = 0;
- }
- break;
- case SniperState.Prepare:
- timer += Time.deltaTime;
- transform.LookAt(player.transform.position);
- if (timer > cooldown)
- {
- _currentState = SniperState.Shoot;
- timer = 0;
- }
- if (Vector3.Distance(transform.position, player.transform.position) > area)
- {
- _currentState = SniperState.Wait;
- }
- break;
- case SniperState.Shoot:
- timer = 0;
- GameObject buf = Instantiate(bullet);
- buf.transform.position = rifleStart.transform.position;
- buf.transform.rotation = transform.rotation;
- buf.GetComponent<Bullet>().setDirection(transform.forward);
- buf.GetComponent<Bullet>().MakeSniper();
- _currentState = SniperState.Prepare;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement