// 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); }
// 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 } } }