Created
January 10, 2021 05:44
-
-
Save fredknack/c27a92e39727d197f3eb3205bca140bf to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MoveToGoal : MonoBehaviour | |
{ | |
//public float speed = 0.3f; | |
public float accuracy = 0.01f; | |
public float randX = 1f; | |
public float randY = 1f; | |
public float randZ = 1f; | |
public Transform goal; | |
public float reactGoalX; | |
public float reactGoalY; | |
public float reactGoalZ; | |
void Start() | |
{ | |
} | |
void Awake() | |
{ | |
} | |
// Update is called once per frame | |
void LateUpdate() | |
{ | |
this.transform.LookAt(goal.position); | |
Vector3 direction = goal.position - this.transform.position; | |
if(direction.magnitude > accuracy) | |
this.transform.Translate(direction.normalized * PersistentManagerScript.Instance.speed * Time.deltaTime, Space.World); | |
if (direction.magnitude < accuracy) | |
{ | |
if(PersistentManagerScript.Instance.lives > 0) | |
{ | |
PersistentManagerScript.Instance.lives = PersistentManagerScript.Instance.lives - 1; | |
Destroy(this.transform.gameObject); | |
} else | |
{ | |
if (PersistentManagerScript.Instance.lives == 0) | |
{ | |
//PersistentManagerScript.Instance.lives = PersistentManagerScript.Instance.lives - 1; | |
PersistentManagerScript.Instance.deathSequence(); | |
Destroy(this.transform.gameObject); | |
} | |
} | |
print("Go Boom! " + PersistentManagerScript.Instance.lives); | |
} | |
} | |
public void react() | |
{ | |
randX = Random.Range(-1.5f, 1.5f); | |
randY = Random.Range(-1.5f, 1.5f); | |
randZ = Random.Range(-0.5f, 0.5f); | |
this.reactGoalX = this.transform.position.x + randX; | |
this.reactGoalY = this.transform.position.y + randY; | |
this.reactGoalZ = this.transform.position.z + randZ; | |
Vector3 reactGoal = new Vector3(reactGoalX, reactGoalY, reactGoalZ); | |
StartCoroutine(MoveFromTo(this.transform, this.transform.position, reactGoal, 2)); | |
} | |
IEnumerator MoveFromTo(Transform goal, Vector3 a, Vector3 b, float speed) | |
{ | |
//Vector3 reactGoal = new Vector3(goal.position.x, goal.position.y, goal.position.z); | |
print("MoveFromTo"); | |
Vector3 reactGoal = new Vector3(0, 1, 0); | |
float step = (speed / (a - b).magnitude) * Time.fixedDeltaTime; | |
float t = 0; | |
while (t <= 1.0f) | |
{ | |
t += step; // Goes from 0 to 1, incrementing by step each time | |
goal.position = Vector3.Lerp(a, b, t); // Move objectToMove closer to b | |
yield return new WaitForFixedUpdate(); // Leave the routine and return here in the next frame | |
} | |
goal.position = b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment