void Update() { if (Rope == null) { Rope = gameObject.AddComponent(typeof(LineRenderer)) as LineRenderer; //Rope.materials[0]. Rope.materials[0].shader = Shader.Find("Bumped Specular"); Rope.materials[0].SetColor("_EmisColor", Color.white); Rope.SetWidth(0.1f, 0.1f); } RopePts.Clear(); RopePts.Add(start.transform.position); RopePts.Add(start.transform.position); for (int i = 0; i < RopeSegs.Count; i++) { RopePts.Add(RopeSegs[i].transform.position); } RopePts.Add(end.transform.position); RopePts.Add(end.transform.position); SplinePts = Spline_Catmull_Rom.GetPoints(RopePts.ToArray(), interp); UpdateRope(); }
//TODO Change time to a constant speed based on rough estimations of distance. //TODO Add an interrupt variable so that the AI can leave the path or change paths. Maybe check to see if destination changes. IEnumerator PathTravel(pathNode location, pathNode destination, float speed) { List <pathNode> path = Dijkstra.ShortestPath(NodePath, location.ID, destination.ID); if (path.Count > 1) { //Add initial location twice to initialize spline cap path.Insert(0, path[0]); //Add destination twice for the end cap on spline path.Add(path[path.Count - 1]); float timer = 0; for (int i = 0; i < path.Count - 3; i++) { timer = 0; pathNode nextNode = path[i + 2]; float dist = Spline_Catmull_Rom.LengthofCurve(path[i].position, path[i + 1].position, path[i + 2].position, path[i + 3].position, 5); float time = dist / speed; while (Vector3.Distance(transform.position, nextNode.position) > Time.deltaTime) { timer += Time.deltaTime; if (timer > time) { break; } transform.position = Spline_Catmull_Rom.PointOnCurve(path[i].position, path[i + 1].position, path[i + 2].position, path[i + 3].position, timer / time); transform.LookAt(Spline_Catmull_Rom.PointOnCurve(path[i].position, path[i + 1].position, path[i + 2].position, path[i + 3].position, (timer + Time.deltaTime) / time)); yield return(null); } } currentNode = destination.ID; traveling = false; } }
//Points in the direction of the path toward ID X public GameObject GetDirectionalArrow(int ID) { List <pathNode> path = Dijkstra.ShortestPath(NodePath, currentNode, ID); GameObject tempObj = Instantiate(arrow) as GameObject; //Add initial location twice to initialize spline cap path.Insert(0, path[0]); //Add destination twice for the end cap on spline path.Add(path[path.Count - 1]); tempObj.transform.position = Spline_Catmull_Rom.PointOnCurve(path[0].position, path[1].position, path[2].position, path[3].position, 0.3f); tempObj.transform.LookAt(Spline_Catmull_Rom.PointOnCurve(path[0].position, path[1].position, path[2].position, path[3].position, 0.5f)); return(tempObj); }
void OnDrawGizmos() { if (NodePath != null) { if (NodePath.Graphs[0].GetNodeList().Count > 0) { if (currentNode != tempCurr && currentNode != destinationNode) { //Debug.Log ("Drawing spline between "+currentNode+" and "+destinationNode); List <pathNode> path = NodePath.ShortestPath(currentGraph, currentNode, destinationGraph, destinationNode); if (path.Count > 0) { path.Insert(0, path[0]); path.Add(path[path.Count - 1]); PathPts.Clear(); for (int i = 0; i < path.Count; i++) { PathPts.Add(path[i].position); } SplinePts.Clear(); if (PathPts.Count > 0) { tempCurr = currentNode; SplinePts = Spline_Catmull_Rom.GetPoints(PathPts.ToArray(), smoothing); } } } for (int i = 0; i < SplinePts.Count - 1; i++) { Debug.DrawLine(SplinePts[i], SplinePts[i + 1], Color.green, 0, false); } /* * for(int i=0;i<NodePath.NodeCount;i++){ * Gizmos.color=Color.blue; * Gizmos.DrawSphere(NodePath.GetNodeList()[i].position,0.1f); * } */ } } }