// In this function we actually move each assigned unit of a grid point towards that moving (in most cases) grid point // We use the Rigidbody velocity and the velocity is calculated based on the distance from the unit to the grid point. public void MoveUnitsRigidBodyMode(int gridPointIndex, FormationGridPoint fgp, Vector3 vlcity, float endReachedDistance) { Rigidbody rigidbody = fgp.GetRigidbody(); if (!rigidbody) { Debug.LogError("FormationGrid.MoveUnitsRigidBodyMode(): Rigidbody missing on assigned unit."); } Vector3 acceleration = fgp.GetPosition() - fgp.GetAssignedUnit().transform.position; acceleration.y = 0; acceleration.Normalize(); acceleration *= maximumAcceleration; //DebugPanel.Log("Acceleration "+gridPointIndex, "Rigidbody", acceleration.magnitude); rigidbody.velocity += acceleration * Time.deltaTime; if (rigidbody.velocity.magnitude > maximumVelocity) { rigidbody.velocity = rigidbody.velocity.normalized * maximumVelocity; } //DebugPanel.Log("Rgb velocity "+gridPointIndex, "Rigidbody", rigidbody.velocity.magnitude); fgp.SetAssignedVelocity(rigidbody.velocity); }
// In this function we actually move each assigned unit of a grid point towards that moving (in most cases) grid point // We use the Move function of the CharacterController and the motion is calculated based on the distance from the unit to the grid point. public void MoveUnitsCharacterControllerMode(int gridPointIndex, FormationGridPoint fgp, Vector3 vlcity, float endReachedDistance) { CharacterController controller = fgp.GetCharacterController(); if (!controller) { Debug.LogError("FormationGrid.MoveUnitsCharacterControllerMode(): Character Controller missing on assigned unit."); } float distanceToGridPoint = fgp.CalculateDistanceUnitToGridPoint(); //if (gridPointIndex == 0) DebugPanel.Log("GridPoint [" + gridPointIndex + "] unittogrid", "Grid", distanceToGridPoint); // default acceleration multiplier float acceleration = 1.0F; if (distanceToGridPoint > endReachedDistance * 5) { // takeover acceleration = accelerationStraggler; } else if ((distanceToGridPoint > endReachedDistance) && ((distanceToGridPoint < endReachedDistance * 5))) { // slowdown float slope = 1 / (4 * endReachedDistance); // 1 / ((5-1) * endReachedDistance) float intercept = -1 * (slope * endReachedDistance); // acceleration = slope * distanceToGridPoint + intercept; // a = 0 at endReachedDistance and a = 1 at 5*endReachedDistance //acceleration = distanceToGridPoint / 0.5F; } else if (distanceToGridPoint < endReachedDistance) { acceleration = 0.0f; } //if (gridPointIndex == 0) DebugPanel.Log("GridPoint [" + gridPointIndex + "] acceleration", "Grid", acceleration); Vector3 direction = fgp.GetPosition() - fgp.GetAssignedUnit().transform.position; Vector3 fgp_velocity = direction * acceleration; if (useGravity) { // Use gravity and calculate vertical velocity down float vSpeed = fgp.GetUnitVerticalSpeed(); if (controller.isGrounded) { vSpeed = 0; } vSpeed -= gravity * Time.deltaTime; fgp.SetUnitVerticalSpeed(vSpeed); fgp_velocity.y = vSpeed; } controller.Move(fgp_velocity * Time.deltaTime); fgp.SetAssignedVelocity(fgp_velocity); }
// Change the movement state of the units assigned to a grid point: // False = stop moving // True = start moving public void ChangeMoveStateOnGridObjects(bool state) { for (int i = 0; i < gridPoints.Count; i++) { FormationGridPoint fgp = gridPoints[i]; GameObject go = fgp.GetAssignedUnit(); if (go) { #if T7T_ASTAR AIPath aip = go.GetComponent <AIPath>(); if (aip) { aip.target = fgp.GetTransform(); aip.canSearch = state; aip.canMove = state; } else { Debug.LogError("FormationGrid.EnableMoveOnGridObjects(): no assigned unit found for gridpoint."); } #else NavMeshAgent nma = go.GetComponent <NavMeshAgent>(); if (nma) { if (state) { nma.destination = fgp.GetPosition(); nma.Resume(); } else { nma.Stop(); Rigidbody rigidbody = fgp.GetRigidbody(); if (rigidbody) { rigidbody.velocity = Vector3.zero; } } } else { Debug.LogError("FormationGrid.EnableMoveOnGridObjects(): no nav mesh agent found for assigned unit."); } #endif } } }
// Assign the objects in a list to the FormationGridPoint(s) in the gridPoints list public bool AssignObjectsToGrid(List <GameObject> list) { bool result = true; if (list.Count > gridPoints.Count) { Debug.LogWarning("FormationGrid.AssignObjectsToGrid(): too many objects for this grid."); result = false; } for (int i = 0; i < list.Count; i++) { if (i < gridPoints.Count) { GameObject go = list[i]; // Now check if the required components are available so we can move the objects if (movementType == MovementType.CharacterController) { CharacterController cc = go.GetComponent <CharacterController>(); if (!cc) { Debug.LogError("FormationGrid.AssignObjectsToGrid(): GameObject to be assigned does not have the required CharacterController for this movement type."); } } else if (movementType == MovementType.RigidBody) { Rigidbody rb = go.GetComponent <Rigidbody>(); if (!rb) { Debug.LogError("FormationGrid.AssignObjectsToGrid(): GameObject to be assigned does not have the required RigidBody for this movement type."); } } #if T7T_ASTAR AIPath aip = go.GetComponent <AIPath>(); if (aip) { FormationGridPoint fgp = gridPoints[i]; fgp.AssignUnit(go); aip.target = fgp.GetTransform(); aip.canMove = true; Debug.Log("FormationGrid.AssignObjectsToGrid(): Assigned new target to object " + go.transform.name); } else { Debug.LogWarning("FormationGrid.AssignObjectsToGrid(): Assigned Object [" + go.transform.name + "] has no AIPath component."); result = false; } #else NavMeshAgent nma = go.GetComponent <NavMeshAgent>(); if (nma) { FormationGridPoint fgp = gridPoints[i]; fgp.AssignUnit(go); nma.destination = fgp.GetPosition(); Debug.Log("FormationGrid.AssignObjectsToGrid(): Assigned new target to object " + go.transform.name); } else { Debug.LogWarning("FormationGrid.AssignObjectsToGrid(): Assigned Object [" + go.transform.name + "] has no Navmesh component."); result = false; } #endif } } return(result); }