Hello, I’m working on a Tower Defense Game, and I’m unsure of which method to use to change the Orientation of an Enemy on the server. I’m currently using the lookVector to make the enemy move in the direction it is pointing every frame, but I’m not sure what would be the way to Create a smooth turn that would change by a certain amount every frame when the enemy position is close enough to a node. Note that I know its bad to change the positions of anything on the server every frame. This question is only for changing the position and orientation of the enemy on the server.
How would I change the orientation smoothly depending on the speed but not in one single Frame?
Move Function that fires every Heartbeat:
local function Move(Enemy: EnemyType, DeltaTime: number)
local self = Enemy;
local Primary = Enemy.Object.PrimaryPart
if not Primary then
return;
end
local Speed = self.Speed;
local CurrentPoint = self.CurrentPoint;
local NextPoint = WayPoints[CurrentPoint]
local LookVector = Primary.CFrame.LookVector;
local PrimaryPosition: Vector3 = Vector3.new(Primary.Position.X, 0, Primary.Position.Z);
local WaypointPosition: Vector3 = Vector3.new(NextPoint.Position.X, 0, NextPoint.Position.Z);
local Distance: Vector3 = (PrimaryPosition - WaypointPosition) :: Vector3;
Primary.CFrame += LookVector * Speed*DeltaTime;
if Distance.Magnitude < 1 then
local PointAfter = WayPoints[CurrentPoint+1];
local TurnLength = 90/Speed*DeltaTime;
--local TurnDirection = ???
end
end
The Heartbeat Connection:
RunService.Heartbeat:Connect(function(DeltaTime)
for _, _Enemy in ipairs(Enemies) do
local self = _Enemy;
local CurrentWayPoint = WayPoints[self.CurrentPoint];
if not self.Object.PrimaryPart then
return;
end
local Primary = self.Object.PrimaryPart;
local Speed = self.Speed;
local YCordinate = CurrentWayPoint.Position.Y+(self.Object:GetExtentsSize().Y/2)
local PrimaryPosition: Vector3 = Vector3.new(Primary.Position.X, YCordinate, Primary.Position.Z)
local WayPointPosition: Vector3 = Vector3.new(CurrentWayPoint.Position.X, YCordinate, CurrentWayPoint.Position.Z)
local Distance = PrimaryPosition-WayPointPosition;
local Unit = Distance.Unit;
local Magntiude = Distance.Magnitude;
local Ratio: number = Magntiude/Speed
Move(self, DeltaTime)
end
end)