Created
October 27, 2017 09:32
-
-
Save rdeioris/b4f3acc19e8dc586096a87fe480e7d15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class MoveTowards : MonoBehaviour | |
{ | |
public float speed; | |
public Transform myBase; | |
// Use this for initialization | |
void Start() | |
{ | |
} | |
bool OwnsAMineral() | |
{ | |
Mineral[] ownedMinerals = GetComponentsInChildren<Mineral>(); | |
if (ownedMinerals.Length > 0) | |
return true; | |
return false; | |
} | |
void ToMineral() | |
{ | |
FindBestMineral component = GetComponent<FindBestMineral>(); | |
if (component == null) | |
return; | |
Mineral bestMineral = component.GetNearestMineral(); | |
if (bestMineral == null) | |
return; | |
Vector3 target = bestMineral.transform.position; | |
Vector3 direction = (target - transform.position).normalized; | |
transform.position += direction * speed * Time.deltaTime; | |
float distanceFromTarget = (target - transform.position).magnitude; | |
if (distanceFromTarget < 0.1f) | |
{ | |
bestMineral.transform.SetParent(transform); | |
bestMineral.wild = false; | |
} | |
} | |
void ToBase() | |
{ | |
Vector3 target = myBase.position; | |
Vector3 direction = (target - transform.position).normalized; | |
transform.position += direction * speed * Time.deltaTime; | |
float distanceFromTarget = (target - transform.position).magnitude; | |
if (distanceFromTarget < 0.1f) | |
{ | |
Mineral[] ownedMinerals = GetComponentsInChildren<Mineral>(); | |
foreach (Mineral mineral in ownedMinerals) | |
{ | |
mineral.transform.SetParent(null); | |
} | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (OwnsAMineral()) | |
{ | |
ToBase(); | |
} | |
else | |
{ | |
ToMineral(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment