public void PlaceCubeOfDeath(ref GameObject cubeParent, Vector3 position, Vector3 scale, Vector3 mazeFrameScale, MazeFrameSplines mazeFrameSplines) { if (cubeParent != null) { Object.Destroy(cubeParent); cubeParent = null; } cubeParent = new GameObject("CubesOfDeath"); GameObject cube = Object.Instantiate(Resources.Load("Prefabs/" + "CubeOfDeath"), cubeParent.transform) as GameObject; cube.transform.position = position; cube.transform.localScale = Vector3.zero; cube.name = "CubeOfDeath"; CubeOfDeathController cubeOfDeathController = cube.GetComponent <CubeOfDeathController>(); CubeOfDeathController.MazeScale = mazeFrameScale; CubeOfDeathController.MazeFrameSplines = mazeFrameSplines; CubeOfDeathController.cubeScale = scale; cubeOfDeathController.Activate(); //cube.SetActive(true); }
/// <summary> /// Translate cube along splines to follow player, and replicate itself at junctions /// </summary> private void MoveCubeAlongSplineAndReplicate() { // Get current distance value float currD = CurrSplineSegment.spline.GetDistanceOnSplineFromPosition(CODTrans.position, ReversedSpline); // Project position along spline float projD; if (goingForward) { projD = currD + (moveSpeed * Time.deltaTime); } else { projD = currD - (moveSpeed * Time.deltaTime); } // Check whether end is reached bool reachedEnd = false; if (goingForward) { if (projD > CurrSplineSegment.spline.SplineTotalDistance) { reachedEnd = true; } } else { if (projD < 0) { reachedEnd = true; } } // If end is reached, reverse direction if no untouched segments are found, otherwise, continue along first // segment and replicate cube and move them along the others if (reachedEnd) { // See if other segment is present List <MazeFrameSplines.SplineSegment> splineNeighbors; if (!ReversedSpline) { splineNeighbors = new List <MazeFrameSplines.SplineSegment>(CurrSplineSegment.endNeighbors); } else { splineNeighbors = new List <MazeFrameSplines.SplineSegment>(CurrSplineSegment.startNeighbors); } // Discard splines already being travelled for (int i = splineNeighbors.Count - 1; i >= 0; i--) { if (cubeHasBeenHere[splineNeighbors[i]]) { splineNeighbors.RemoveAt(i); } } // Turn around if none present if (splineNeighbors.Count == 0) { if (goingForward) { projD = CurrSplineSegment.spline.SplineTotalDistance; goingForward = false; } else { projD = 0; goingForward = true; } } else { // Instantiate new cube for every neighbor that hasn't been visisted yet, except the first for (int i = 0; i < splineNeighbors.Count; i++) { cubeHasBeenHere[splineNeighbors[i]] = true; } for (int i = 1; i < splineNeighbors.Count; i++) { GameObject cube = Instantiate(gameObject, CODTrans.position, CODTrans.rotation, gameObject.transform.parent); CubeOfDeathController CODcont = cube.GetComponent <CubeOfDeathController>(); CODcont.CurrSplineSegment = splineNeighbors[i]; CODcont.IsOnSpline = true; if (Vector3.Distance(CODTrans.position, CODcont.CurrSplineSegment.EndPoint) < Vector3.Distance(CODTrans.position, CODcont.CurrSplineSegment.StartPoint)) { CODcont.ReversedSpline = true; } else { CODcont.ReversedSpline = false; } } // For this cube, continue on new segment with remainder of projected distance projD = projD - CurrSplineSegment.spline.SplineTotalDistance; CurrSplineSegment = splineNeighbors[0]; if (Vector3.Distance(CODTrans.position, CurrSplineSegment.EndPoint) < Vector3.Distance(CODTrans.position, CurrSplineSegment.StartPoint)) { ReversedSpline = true; } else { ReversedSpline = false; } } } float projT = CurrSplineSegment.spline.GetTAtDistance(projD, ReversedSpline); CODTrans.position = CurrSplineSegment.spline.GetPointOnSpline(projT); CODTrans.rotation = Quaternion.LookRotation(CurrSplineSegment.spline.GetTangentToPointOnSpline(projT), CurrSplineSegment.spline.DefaultGetNormalAtT(projT)); }