private void moveKinematic() { if (Input.GetMouseButtonDown(0)) { destination = DirectionUtility.getMouseDirection(); destination.y = transform.position.y; go = true; calculating = true; inizializeTree(destination); draw.clean(); } else { if (go && path.Count > 0) { destination = (Vector3)path[path_index]; float d = Vector3.Distance(transform.position, destination); if (d > 0.5) { Vector3 direction = (destination - transform.position).normalized; DirectionUtility.makeKinematicMove(rigidbody, direction, speed); } else { rigidbody.velocity = Vector3.zero; transform.position = destination; path_index++; if (path_index == path.Count) { go = false; path_index = 0; } } } } }
// Update is called once per frame void Update() { if (debug_print) { debug_print = false; string log = ""; for (int i = 0; i < path.Count; i++) { log += path[i] + " "; } Debug.Log(log); } if (go && path != null && path.Count != 0) { if (!AstarReady) { // Debug.Log(path[index]); pathAstar = Astar.getPath(transform.position, (Vector3)path[index]); pathAstar.Reverse(); pathAstar.Add(path[index]); //if(printAstarPath) foreach (Vector3 p in pathAstar) createPoint(p); if (printAstarPath) { draw.drawMultipleLines(pathAstar, Color.red); } AstarReady = true; indexAstar = 0; //Debug.Log("Count "+pathAstar.Count); } if (pathAstar == null || pathAstar.Count == 0) { index++; if (index == path.Count) { go = false; index = 0; } return; } Vector3 destination = (Vector3)pathAstar[indexAstar]; //Debug.Log(indexAstar); destination.y = mainY; float d = Vector3.Distance(transform.position, destination); if (d > 0.5f) { //makeDifferentialMove (transform, rigidbody, destination, 15f, 10f); Vector3 direction = (destination - transform.position).normalized; DirectionUtility.makeKinematicMove(rigidbody, direction, speed); } else { //Debug.Log(index); transform.position = destination; //snap to destination rigidbody.velocity = Vector3.zero; rigidbody.angularVelocity = Vector3.zero; indexAstar++; if (indexAstar == pathAstar.Count) { //go = false; // Debug.Log(indexAstar); indexAstar = 0; AstarReady = false; index++; if (index == path.Count) { go = false; index = 0; } } } } }
void Update() { if (Input.GetMouseButtonDown(0)) { destination = DirectionUtility.getMouseDirection(); destination.y = transform.position.y; calcPath = true; } if (calcPath) { calcPath = false; go = true; path = car.RSR(transform.position, transform.forward, destination); draw.drawMultipleLines(path, Color.magenta); if (free(path)) { return; } path = car.LSR(transform.position, transform.forward, destination); draw.drawMultipleLines(path, Color.magenta); if (free(path)) { return; } path = car.LSL(transform.position, transform.forward, destination); draw.drawMultipleLines(path, Color.magenta); if (free(path)) { return; } path = car.RSL(transform.position, transform.forward, destination); draw.drawMultipleLines(path, Color.magenta); if (free(path)) { return; } go = false; } else if (go && path.Count > 0) { destination = (Vector3)path[path_index]; float d = Vector3.Distance(transform.position, destination); if (d > 2) { Vector3 direction = (destination - transform.position).normalized; correctAngle(direction); DirectionUtility.makeKinematicMove(rigidbody, direction, speed); } else { rigidbody.velocity = Vector3.zero; transform.position = destination; path_index++; if (path_index == path.Count) { go = false; path_index = 1; } } } }