Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- [RequireComponent(typeof(ParticleSystem))]
- [RequireComponent(typeof(LineRenderer))]
- public class TrackingSystem : MonoBehaviour {
- GameObject target;
- GameObject cube;
- LineRenderer gunLine;
- ParticleSystem gunParticles;
- Light gunLight;
- LayerMask playerMask;
- LayerMask cubeMask;
- Vector3 startPosition;
- Ray shootRay;
- RaycastHit hit;
- int shotCounter = 50;
- float shotDuration = 3f;
- float timeBetweenShots = 50f;
- float range = 20f;
- float angleRange = 40;
- float firstAngle;
- float maxAngle;
- float angle;
- bool foundTarget;
- [SerializeField]
- bool setHanging;
- [SerializeField]
- bool setPatrolling;
- bool isPatrolling;
- bool isShooting;
- bool wasShooting;
- void Awake() {
- gunParticles = GetComponent<ParticleSystem>();
- gunLight = GetComponentInChildren<Light>();
- gunLine = GetComponent<LineRenderer>();
- target = GameObject.FindGameObjectWithTag("Player");
- playerMask = LayerMask.GetMask("Player");
- cubeMask = LayerMask.GetMask("Cube");
- }
- void Start() {
- startPosition = transform.position;
- firstAngle = transform.eulerAngles.y;
- maxAngle = firstAngle + angleRange;
- gunLine.startColor = Color.red;
- gunLine.endColor = Color.red;
- gunLine.SetPosition(0, transform.position);
- if (setPatrolling)
- {
- isPatrolling = true;
- }
- }
- void Update() {
- CheckForRaycast();
- CheckForShooting();
- CountForShooting();
- if (!isShooting)
- {
- DrawLaser();
- }
- if (!isPatrolling && foundTarget)
- {
- StartCoroutine(RotateToTarget());
- }
- if (!foundTarget && isPatrolling)
- {
- StartCoroutine(Patrol());
- }
- }
- void CountForShooting() {
- if (isShooting && (shotCounter == shotDuration))
- {
- DisableEffects();
- }
- else if (isShooting && shotCounter == timeBetweenShots)
- {
- Shoot();
- }
- else if (shotCounter > timeBetweenShots)
- {
- shotCounter = 0;
- }
- shotCounter++;
- }
- void CheckForRaycast() {
- if (setHanging)
- {
- if (Physics.Raycast(transform.position, transform.position + transform.forward * range - target.transform.position, out hit, range, playerMask))
- {
- isShooting = true;
- foundTarget = true;
- }
- }
- else
- {
- if (Physics.Raycast(transform.position, transform.position + transform.forward * range - target.transform.position + new Vector3(0, 1), out hit, range, playerMask))
- {
- isShooting = true;
- foundTarget = true;
- }
- }
- }
- void CheckForShooting() {
- if (isShooting && (maxAngle >= transform.eulerAngles.y || firstAngle - angleRange <= transform.eulerAngles.y))
- {
- if (setPatrolling)
- {
- isPatrolling = false;
- }
- StopAllCoroutines();
- StartCoroutine(RotateToTarget());
- }
- if (!isShooting && wasShooting)
- {
- if (setPatrolling)
- {
- isPatrolling = true;
- }
- StopAllCoroutines();
- StartCoroutine(BackToFirstPosition());
- }
- }
- public void DisableEffects() {
- gunLine.enabled = false;
- gunLight.enabled = false;
- }
- void DrawLaser() {
- gunLine.enabled = true;
- if (Physics.Raycast(transform.position, transform.forward, out hit, range, cubeMask))
- {
- gunLine.SetPosition(1, hit.point);
- }
- else
- {
- if (setHanging && !setPatrolling)
- {
- gunLine.SetPosition(1, transform.position + transform.forward * range + new Vector3(0,-4f));
- }
- else
- {
- gunLine.SetPosition(1, transform.position + transform.forward * range);
- }
- }
- }
- void Shoot() {
- wasShooting = false;
- shotCounter = 0;
- gunParticles.Stop();
- gunParticles.Play();
- gunLight.enabled = true;
- gunLine.enabled = true;
- if (Physics.Raycast(shootRay, out hit, range))
- {
- gunLine.SetPosition(1, hit.point);
- }
- else
- {
- gunLine.SetPosition(1, transform.position + transform.forward * range);
- }
- }
- public IEnumerator RotateToTarget() {
- for (float t = 0.0f; t < 1.0f;)
- {
- t += Time.deltaTime * 0.8f;
- Quaternion rotate = Quaternion.LookRotation(target.transform.position - transform.position);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotate, t);
- if (firstAngle + angleRange <= transform.eulerAngles.y || firstAngle - angleRange >= transform.eulerAngles.y)
- {
- isShooting = false;
- wasShooting = true;
- foundTarget = false;
- }
- yield return null;
- }
- }
- public IEnumerator Patrol() {
- for (float t = 0.0f; t < 1.0f;)
- {
- t += Time.deltaTime * 0.1f;
- angle = Mathf.LerpAngle(firstAngle, maxAngle, t);
- if (setHanging)
- {
- transform.eulerAngles = new Vector3(10, angle, 0);
- }
- else
- {
- transform.eulerAngles = new Vector3(0, angle, 0);
- }
- yield return null;
- }
- yield return StartCoroutine(GoBack());
- }
- public IEnumerator GoBack() {
- for (float t = 0.0f; t < 1.0f;)
- {
- t += Time.deltaTime * 0.1f;
- angle = Mathf.LerpAngle(maxAngle, firstAngle - angleRange, t);
- if (setHanging)
- {
- transform.eulerAngles = new Vector3(10, angle, 0);
- }
- else
- {
- transform.eulerAngles = new Vector3(0, angle, 0);
- }
- yield return null;
- }
- yield return StartCoroutine(GoAgain());
- }
- public IEnumerator GoAgain() {
- for (float t = 0.0f; t < 1.0f;)
- {
- t += Time.deltaTime * 0.1f;
- angle = Mathf.LerpAngle(firstAngle - angleRange, maxAngle, t);
- if (setHanging)
- {
- transform.eulerAngles = new Vector3(10, angle, 0);
- }
- else
- {
- transform.eulerAngles = new Vector3(0, angle, 0);
- }
- yield return null;
- }
- yield return StartCoroutine(GoBack());
- }
- public IEnumerator BackToFirstPosition() {
- for (float t = 0.0f; t < 1.0f;)
- {
- t += Time.deltaTime * 0.1f;
- angle = Mathf.LerpAngle(transform.eulerAngles.y, firstAngle, t);
- if (setHanging)
- {
- transform.eulerAngles = new Vector3(10, angle, 0);
- }
- else
- {
- transform.eulerAngles = new Vector3(0, angle, 0);
- }
- foundTarget = false;
- if (setPatrolling)
- {
- isPatrolling = true;
- }
- yield return null;
- }
- if (setPatrolling)
- {
- yield return StartCoroutine(Patrol());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement