public static void Stop(this MoveComponent self) { if (self.Targets.Count > 0) { self.MoveForward(true); } self.Clear(); }
public static void MoveForward(this MoveComponent self, bool needCancel) { Unit unit = self.GetParent <Unit>(); long timeNow = TimeHelper.ClientNow(); long moveTime = timeNow - self.StartTime; while (true) { if (moveTime <= 0) { return; } // 计算位置插值 if (moveTime >= self.NeedTime) { unit.Position = self.NextTarget; if (self.TurnTime > 0) { unit.Rotation = self.To; } } else { // 计算位置插值 float amount = moveTime * 1f / self.NeedTime; if (amount > 0) { Vector3 newPos = Vector3.Lerp(self.StartPos, self.NextTarget, amount); unit.Position = newPos; } // 计算方向插值 if (self.TurnTime > 0) { amount = moveTime * 1f / self.TurnTime; Quaternion q = Quaternion.Slerp(self.From, self.To, amount); unit.Rotation = q; } } moveTime -= self.NeedTime; // 表示这个点还没走完,等下一帧再来 if (moveTime < 0) { return; } // 到这里说明这个点已经走完 // 如果是最后一个点 if (self.N >= self.Targets.Count - 1) { unit.Position = self.NextTarget; unit.Rotation = self.To; Action <bool> callback = self.Callback; self.Callback = null; self.Clear(); callback?.Invoke(!needCancel); return; } self.SetNextTarget(); } }