public void Update(float deltaTime) { if (deltaTime == 0f) { return; } foreach (TravelerData traveler in m_AddTravelers) { m_Travelers.Add(traveler); } m_AddTravelers.Clear(); foreach (TravelerData traveler in m_RemoveTravelers) { m_Travelers.Remove(traveler); } m_RemoveTravelers.Clear(); foreach (TravelerData traveler in m_Travelers) { if (traveler.speed == 0f) { continue; } traveler.deltaDistance = traveler.speed * deltaTime; Vector2 direction = Vector2Utils.DegreeToVector2(traveler.rotation); traveler.position += direction * traveler.deltaDistance; if (traveler.hasDestination) { UpdateDestination(traveler); } if (OnPositionChanged != null) { OnPositionChanged(traveler); } } }
public static float SetDestination(TravelerData traveler, Vector2 destination) { traveler.hasDestination = true; traveler.destination = destination; Vector2 offset = destination - traveler.position; float distance = offset.magnitude; if (distance == 0f) { return(0f); } if (traveler.speed <= 0f) { return(0f); } traveler.rotation = Vector2Utils.AngleBetweenPoints(traveler.position, destination); float duration = distance / traveler.speed; return(duration); }