Snippets
Created by
Jesse Williams
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | using System.Collections;
using UnityEngine;
using UnityEngine.AI;
using Labyrintheer;
using Labyrintheer.AI;
namespace Labyrintheer.Utility
{
[System.Serializable]
public class ObjectThrower : MonoBehaviour
{
public ObjectThrowerType otType;
public float fountainAngle = 45.0f; // angle of launch
public float speed = 1.0f; // meters per second
public float gravity = .5f; // 9.81m/s^2 standard Earth gravity
public GameObject go;
public int numObjects;
public Transform t;
public bool _bool = false;
LLogger logger = LLogger.Instance;
public void Update()
{
if (_bool)
{
_bool = false;
StartCoroutine(ThrowObjects());
//ThrowObjects();
}
}
/// <summary>
/// Begins throwing objects in the game
/// </summary>
/// <returns></returns>
public IEnumerator ThrowObjects()
{
switch (otType)
{
case ObjectThrowerType.Fountain:
for (int i = numObjects; i > 0; i--)
{
StartCoroutine(ThrowFountain());
//ThrowFountain();
}
break;
default:
break;
}
yield return new WaitForEndOfFrame();
}
/// <summary>
/// Throws objects in the game in a fountain pattern
/// </summary>
/// <returns></returns>
public IEnumerator ThrowFountain()
{
System.Random rnd = new System.Random(System.Guid.NewGuid().GetHashCode());
GameObject _go = Transform.Instantiate(go, t.position, Quaternion.identity);
NavMeshAgent agent = _go.GetComponent<NavMeshAgent>();
LAIController lai = _go.GetComponent<LAIController>();
int iterations = 1;
if (lai != null)
lai.enabled = false;
if (agent != null)
agent.enabled = false;
_go.transform.parent = t.parent.transform;
_go.transform.position = t.position + new Vector3(0.0f, 1.5f, 0.0f);
float trajectoryHeight = rnd.Next(3, 6);
bool closeEnough = false;
Vector3 startVec = _go.transform.position;
Vector3 newVec = new Vector3(t.position.x + rnd.Next(-10, 10), t.position.y, t.position.z + rnd.Next(-10, 10));
float distance = Vector3.Distance(startVec, newVec);
float startTime = Time.timeSinceLevelLoad;
float evalTime = startTime;
while (!closeEnough)
{
logger.Log("Iteration: " + iterations.ToString() + "\n\n" + "closeEnough: " + closeEnough.ToString(), LLogLevel.DETAIL, LLogModule.MOB);
float distCovered = (evalTime - startTime) * speed;
float fracJourney = distCovered / distance;
Vector3 currPos = Vector3.Lerp(startVec, newVec, fracJourney);
currPos.y += trajectoryHeight * Mathf.Sin(Mathf.Clamp01(distCovered) * Mathf.PI);
_go.transform.position = currPos;
closeEnough = LUtils.NearlyEqual(currPos, newVec, 0.1f);
evalTime += Time.deltaTime;
iterations++;
yield return new WaitForEndOfFrame();
}
logger.Log("Iterations Complete.", LLogLevel.DETAIL, LLogModule.MOB);
//if (agent != null)
agent.enabled = true;
//if (lai != null)
lai.enabled = true;
yield return new WaitForEndOfFrame();
}
}
public enum ObjectThrowerType
{
NONE = 0,
Fountain = 1,
Ray = 2
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.