public Vector3?GetSteering(SteeringInput input) { if (!this.enabled || !this.gameObject.activeSelf) { // If this component or this game object is disabled, don't do steering return(null); } if (this.path == null) { // no valid path, nothing to do return(null); } if (this.path.Count == 0) { // no path nodes in the path, so null it this.path = null; return(null); } // Get the distance to the next path node, if the distance is lower than the arrival distance, we can move on to the next point by popping on the path var currentDirection = (_currentDestination - this.transform.position); var currentDistance = currentDirection.magnitude; if (currentDistance < this.arrivalDistance) { _currentDestination = this.path.Pop(); return(null); } // Velocity is a vector in the direction from the current location to the next destination, with a length of speed capped to the current distance if we are at the last path node - for slowdown var speed = this.path.Count == 1 ? Mathf.Clamp(input.speed, 1f, Mathf.Max(1f, currentDistance)) : input.speed; var velocity = (currentDirection / currentDistance) * speed; return(velocity); }
public Vector3?GetSteering(SteeringInput input) { // TODO: Add implementation here (and return something different from null!) return(null); }
public Vector3?GetSteering(SteeringInput input) { if (!this.enabled || !this.gameObject.activeSelf) { // If this component or this game object is disabled, don't do steering return(null); } var grid = Grid.instance; var position = _unit.transform.position; var currentCell = grid.GetCell(position); if (currentCell == null || currentCell.blocked) { // cannot resolve when already on an invalid/blocked cell return(null); } // Iterate through all neighbour cells compared to our current cell var avoidVector = Vector3.zero; var xLength = grid.cells.GetLength(0); var zLength = grid.cells.GetLength(1); var xIdx = currentCell.xIndex; var zIdx = currentCell.zIndex; for (int x = -1; x <= 1; x++) { for (int z = -1; z <= 1; z++) { if (x == 0 && z == 0) { // ignore the 'cell at the center', since it is the same as the current cell continue; } var ix = xIdx + x; var iz = zIdx + z; if (ix < 0 || iz < 0 || ix >= xLength || iz >= zLength) { // skip cells that are technically outside of the grid continue; } var cell = grid.cells[ix, iz]; if (cell.blocked) { // sum up all vectors from blocked cells avoidVector += (position - cell.position); } } } if (avoidVector.sqrMagnitude == 0f) { // no avoid vectors accumulated return(null); } return(avoidVector.normalized * input.speed); }