Snippets

Jesse Williams ObjectThrower.cs

Created by Jesse Williams

File snippet.txt Added

  • Ignore whitespace
  • Hide word diff
+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
+	}
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.