Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DroneMove : MonoBehaviour
- {
- public Transform target; //current target. Pass a transform here via different script
- public GameObject carrier; //the carrier this object returns to.
- public float moveSpeed;
- public float rotSpeed;
- public float distToTarget;
- public float acqRange;
- public Vector2[] rndPointList;
- public int pointID;
- public bool newTarget; //set by a different script
- public string obj_Tag;
- //
- DroneManager droneManager;
- //
- void Start()
- {
- pointID = 0;
- GenerateRndPoints();
- }
- public void GenerateRndPoints()
- {
- while (pointID < rndPointList.Length)
- {
- if (pointID < 1)
- {
- rndPointList[pointID] = target.position;
- }
- else if (pointID >= 1)
- {
- rndPointList[pointID].x = target.position.x + Random.Range(-2.5f, 2.5f);
- rndPointList[pointID].y = target.position.y + Random.Range(-2.5f, 2.5f);
- }
- pointID++;
- }
- pointID = 0;
- }
- void Update()
- {
- if (target != null)
- {
- Move();
- FaceTarget();
- }
- else if (target == null)
- {
- ReturnToCarrier();
- FaceCarrier();
- }
- DistanceCheck();
- if (newTarget == true)
- {
- GenerateRndPoints();
- newTarget = false;
- }
- if (carrier == null)
- {
- Destroy(this.gameObject);
- }
- }
- void ReturnToCarrier()
- {
- if (distToTarget > acqRange)
- {
- this.transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
- }
- else if (distToTarget <= acqRange)
- {
- droneManager = carrier.GetComponent<DroneManager>();
- droneManager.drones.Remove(this.gameObject);
- Destroy(this.gameObject);
- }
- }
- void FaceCarrier()
- {
- Vector2 direction = carrier.transform.position - this.transform.position;
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
- Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
- }
- void Move()
- {
- if (distToTarget > acqRange)
- {
- this.transform.Translate(moveSpeed * Time.deltaTime, 0, 0);
- }
- if (distToTarget < acqRange)
- {
- if (pointID < rndPointList.Length - 1)
- {
- pointID++;
- //Debug.Log("Point ID value:" + pointID);
- }
- else if (pointID >= rndPointList.Length - 1)
- {
- pointID = 0;
- GenerateRndPoints();
- }
- }
- }
- void DistanceCheck()
- {
- if (target != null)
- {
- distToTarget = Vector3.Distance(this.transform.position, rndPointList[pointID]);
- }
- else if (target == null)
- {
- distToTarget = Vector3.Distance(this.transform.position, carrier.transform.position);
- }
- }
- void FaceTarget()
- {
- //rotate towards target code
- Vector2 direction = rndPointList[pointID] - (Vector2)transform.position;
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
- Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement