# Bubbles I **Technology**: Agents Moving Within Box Colliders **Application**: VFX ![[DESIGNS/TOYS/Agents_WithinCollider.gif]] #### Relative **See** [[IBU/GAME DESIGN/ToyBox/Bubbles II|Bubbles II]] ![[DESIGNS/TOYS/ObjectClicker__Attracts_and_transforms_objects.gif]] ### Scene Controller ```cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class ColliLab5_Controller : MonoBehaviour { public Color[] agentColours; public GameObject box1; public GameObject box2; public GameObject objPref; private int boxIndex; // Creates 10 objects // Randomly in 1t or 2nd box public void OnButtonCreate() { StartCoroutine(CreationCoroutine()); } private IEnumerator CreationCoroutine() { for (int i = 0; i < 1; i++) { DistributeObjects(); yield return new WaitForSeconds(0.5f); } } private void DistributeObjects() { boxIndex = Random.Range(0, 2); if (boxIndex == 0) Create(box1); else if (boxIndex == 1) Create(box2); } private void Create(GameObject box) { GameObject obj = Instantiate(objPref, DefinePositionInBox(box), Quaternion.identity); // Change colour int cInd = Random.Range(0, agentColours.Length); obj.GetComponent<SpriteRenderer>().color = agentColours[cInd]; // Send the box object to the agent ColliLab5_ObjContr objScr = obj.GetComponent<ColliLab5_ObjContr>(); objScr.ReceiveBox(box); } private Vector3 DefinePositionInBox(GameObject box) { float offsetX = box.GetComponent<BoxCollider2D>().offset.x; float offsetY = box.GetComponent<BoxCollider2D>().offset.y; Vector3 collPos = box.transform.position + new Vector3(offsetX, offsetY, 0); float leftX = collPos.x - (box.GetComponent<BoxCollider2D>().size.x / 2); float rightX = collPos.x + (box.GetComponent<BoxCollider2D>().size.x / 2); float upY = collPos.y + (box.GetComponent<BoxCollider2D>().size.y / 2); float downY = collPos.y - (box.GetComponent<BoxCollider2D>().size.y / 2); Vector3 randomVec = new Vector3(Random.Range(leftX, rightX), Random.Range(downY, upY), 0); return randomVec; } } ``` ### Agent Controller ```cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class ColliLab5_ObjContr : MonoBehaviour { // (1) Move at some position each 3 seconds, // Then change the positions // (2) Assign names obj1 & obj2 to each object from the controller // And then move within a related box based on the name of the object // from the obejct's script // MOVEMENT private float moveSpeed = 1f; private Vector3 target; private float movementLength = 2f; private GameObject theBox; public void ReceiveBox(GameObject box) { theBox = box; } private void Start() { InvokeRepeating("ChangeTarget", 1, movementLength); } private void ChangeTarget() { target = DefineTargetInBox(theBox); } private void Movement() { gameObject.transform.position = Vector3.MoveTowards (gameObject.transform.position, target, moveSpeed * Time.deltaTime); } private Vector3 DefineTargetInBox(GameObject box) { float offsetX = box.GetComponent<BoxCollider2D>().offset.x; float offsetY = box.GetComponent<BoxCollider2D>().offset.y; Vector3 collPos = box.transform.position + new Vector3(offsetX, offsetY, 0); float leftX = collPos.x - (box.GetComponent<BoxCollider2D>().size.x / 2); float rightX = collPos.x + (box.GetComponent<BoxCollider2D>().size.x / 2); float upY = collPos.y + (box.GetComponent<BoxCollider2D>().size.y / 2); float downY = collPos.y - (box.GetComponent<BoxCollider2D>().size.y / 2); Vector3 randomVec = new Vector3(Random.Range(leftX, rightX), Random.Range(downY, upY), 0); return randomVec; } private void Update() { if (theBox != null) Movement(); else return; } } ```