} // Whether the prism is active // Start is called before the first frame update void Start() { // Set color pointer materials colorPointerMaterials = new Material[4]; colorPointerMaterials[0] = (Material)Resources.Load("Materials/RedLight"); colorPointerMaterials[1] = (Material)Resources.Load("Materials/YellowLight"); colorPointerMaterials[2] = (Material)Resources.Load("Materials/GreenLight"); colorPointerMaterials[3] = (Material)Resources.Load("Materials/BlueLight"); // Get color pointers redLine = transform.GetChild(0).GetComponent <ColorPointer>(); yellowLine = transform.GetChild(1).GetComponent <ColorPointer>(); greenLine = transform.GetChild(2).GetComponent <ColorPointer>(); blueLine = transform.GetChild(3).GetComponent <ColorPointer>(); lines = new ColorPointer[4]; lines[0] = redLine; lines[1] = yellowLine; lines[2] = greenLine; lines[3] = blueLine; // Get white pointer whiteLine = transform.GetChild(4).GetComponent <Pointer>(); // Set pointers to inactive foreach (ColorPointer line in lines) { line.Active = false; } whiteLine.Active = false; // Set colored player sprites redPlayer = transform.GetChild(5); yellowPlayer = transform.GetChild(6); greenPlayer = transform.GetChild(7); bluePlayer = transform.GetChild(8); whitePlayer = transform.GetChild(9); playerImages = new List <Transform>(); playerImages.Add(redPlayer); playerImages.Add(yellowPlayer); playerImages.Add(greenPlayer); playerImages.Add(bluePlayer); // Scale images to 1 foreach (Transform image in playerImages) { image.localScale = new Vector3(1 / transform.localScale.x, 1 / transform.localScale.y, transform.localScale.z); } whitePlayer.localScale = new Vector3(1 / transform.localScale.x, 1 / transform.localScale.y, transform.localScale.z); // Set default vars FrameBuffer = 0; Active = false; validRefract = false; LinesToCombine = new List <ColorPointer>(); }
// Refract a ray through a prism protected virtual void Refract() { // Send ray through prism int tries = 0; ray.origin = hit.point; // Move ray into prism while (!hit.transform.GetComponent <PolygonCollider2D>().OverlapPoint(ray.origin) && tries < 1000) { ray.origin += ray.direction * 0.01f; tries++; } if (hit.transform.GetComponent <PolygonCollider2D>().OverlapPoint(ray.origin)) { // Set angle inside prism Vector3 refractedDirection = Quaternion.AngleAxis(-20, Vector3.forward) * ray.direction; ray.direction = refractedDirection; // Move ray out of prism while (hit.transform.GetComponent <PolygonCollider2D>().OverlapPoint(ray.origin)) { ray.origin += ray.direction * 0.01f; } path.Add(ray.origin); // Render line through prism lr.positionCount++; lr.SetPosition(lr.positionCount - 1, ray.origin); // Activate prism hit.transform.GetComponent <Prism>().Active = true; hit.transform.GetComponent <Prism>().FrameBuffer = 1; // Activate prism lights for (int i = 0; i < 4; i++) { // Set color pointer positions and directions ColorPointer colorPointer = hit.transform.GetChild(i).GetComponent <ColorPointer>(); colorPointer.SetStartPoint(ray.origin); Vector3 colorDirection = Quaternion.AngleAxis(-(5 * i), Vector3.forward) * refractedDirection; colorPointer.gameObject.transform.right = colorDirection; } } }