void moveToTarget() { rb.constraints = RigidbodyConstraints2D.FreezeRotation; Vector2 arriveAccel = steeringUtils.arrive(arriveTarget); steeringUtils.steer(arriveAccel); steeringUtils.lookWhereYoureGoing(); }
public Vector2 getSteering(LinePath path) { Vector2 targetPosition; // If the path has only one node then just go to that position; if (path.Length == 1) { targetPosition = path[0]; } // Else find the closest spot on the path to the character and go to that instead. else { /* Find the final destination of the character on this path */ Vector2 finalDestination = (pathDirection > 0) ? path[path.Length - 1] : path[0]; /* If we are close enough to the final destination then either stop moving or reverse if * the character is set to loop on paths */ if (Vector2.Distance(transform.position, finalDestination) < stopRadius) { if (pathLoop) { pathDirection *= -1; } else { rb.velocity = Vector2.zero; return(Vector2.zero); } } /* Get the param for the closest position point on the path given the character's position */ float param = path.getParam(transform.position); /* Move down the path */ param += pathDirection * pathOffset; /* Make sure we don't move past the beginning or end of the path */ if (param < 0) { param = 0; } else if (param > path.maxDist) { param = path.maxDist; } /* Set the target position */ targetPosition = path.getPosition(param); } return(steeringUtils.arrive(targetPosition)); }
// Update is called once per frame void Update() { if (enemy.stunTill > Time.time) { attacking = false; GetComponent <Rigidbody2D>().velocity = Vector2.zero; return; } if (player != null) { Vector2 sepAccel = steeringUtils.separation("Enemy"); Vector2 arriveAccel = steeringUtils.arrive(player.position); if (sepAccel != Vector2.zero) { steeringUtils.steer(sepAccel); } else if (arriveAccel != Vector2.zero) { steeringUtils.steer(arriveAccel); } attacking = (Vector3.Distance(transform.position, player.position) <= steeringUtils.targetRadius); } // Else the player is dead so stop attacking and stop moving else { attacking = false; GetComponent <Rigidbody2D>().velocity = Vector2.zero; } updateMeleeAttack(); steeringUtils.lookWhereYoureGoing(); }
internal void moveUnit() { if (target != null) { findPathToUnit(); } Vector2 accel = Vector2.zero; bool standStill = false; if (currentPath != null) { Vector2 targetPosition; accel = steerTowardsPath(out targetPosition); int[] mapPos = Map.map.worldToMapPoint(targetPosition); if (!equals(mapPos, reservedPos)) { if (Map.map.getObj(mapPos) == null) { Map.map.setObj(reservedPos, null); Map.map.setObj(mapPos, this); reservedPos = mapPos; } else { //Debug.Log (name + " is moving onto an occupied node"); int i = findNextUnoccupiedNode(targetPosition); // Should prob change the if to find path to target and get as close as possible // and make the else code happen outside of the else block if (i == currentPath.Length) { //Debug.Log(name + " has no unoccupied nodes on its current path to its goal"); standStill = true; } else { Vector3 startPos = Map.map.mapToWorldPoint(reservedPos[0], reservedPos[1]); Vector3 endPos = currentPath[i]; LinePath detour = AStar.findPath(Map.map, startPos, endPos, null, 0, false); /* If we can't find a detour path just find a way to the end node */ if (detour == null) { //Debug.Log (name + " no detour to next open space. Finding new path to end goal all together."); currentPath = AStar.findPath(Map.map, currentPath[0], currentPath.endNode, null, 0, false); } /* Else update the current path */ else { //Vector3[] newNodes = new Vector3[detour.Length + (currentPath.Length - i - 1)]; List <Vector3> newNodes = new List <Vector3>(); for (int j = 0; j < currentPath.Length; j++) { if (currentPath[j] != startPos) { newNodes.Add(currentPath[j]); } else { break; } } for (int j = 0; j < detour.Length; j++) { newNodes.Add(detour[j]); } i = i + 1; for (; i < currentPath.Length; i++) { newNodes.Add(currentPath[i]); } currentPath = new LinePath(newNodes.ToArray()); } accel = steerTowardsPath(out targetPosition); } } } } else { standStill = true; } if (standStill) { accel = steeringUtils.arrive(Map.map.mapToWorldPoint(reservedPos [0], reservedPos [1])); } steeringUtils.steer(accel); steeringUtils.lookWhereYoureGoing(); }