so i am writing a troop movement system (for my upcoming grand strategy game) but the problem is that the troops (while moving) would not rotate by the Y axis to look towards the province they’re moving to (i have tried lookAt() but that did not rotate the troop by the Y axis only and it kept the rotation fixed everytime the same troop moves again)
when i don’t include lookAt() the troop keeps their orientation at 0,0,0 by default (obviously since it’s a new CFrame value)
troops move like normal, only problem is the orientation of the troops
here’s the block of code that manages troop movement:
local mouse = game.Players.LocalPlayer:GetMouse()
local hasClicked = false
local TS = game:GetService("TweenService")
mouse.Button1Up:Connect(function()
if hasClicked == true then
local mouseHit = mouse.Target
if mouseHit.Name == "Province" then
hasClicked = false
for _, v in pairs(game.Workspace:GetDescendants()) do
local troop
if v.Name == "SelectedHighlight" then
troop = v.Parent
local distance = (troop.PrimaryPart.Position - mouseHit.Position).Magnitude
local timeToMove = distance / 2
local movement = TweenInfo.new(
timeToMove,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
)
local props = {
CFrame = CFrame.new(Vector3.new(mouseHit.Position.X, troop.PrimaryPart.Position.Y, mouseHit.Position.Z))
}
TS:Create(troop.PrimaryPart, movement, props):Play()
end
if v:IsA("Highlight") then
v:Destroy()
end
end
end
end
end)
thanks for the support you’re providing me but the troop flies off the map when i do that (the Y axis position gets to a very high number (close to math.huge) for the troop that’s getting moved)
that rotates the troop in the X and Z axis (which i don’t want to since it would flip the troop on the sides instead if the Y lookat is 0, but it would still move the troop normally though) and without the lookat parameter it would keep the orientation at 0,0,0 and would normally move the troop (which i also don’t want since the troop would not rotate at all and would be weird for players)
even when i kept the y vector3 for the lookat to 0, it still rotated it in the y axis
the troop also stands on the sides of itself after it finishes rotating and the troop will not rotate anymore when it gets moved again (basically it will remain it’s orientation at -90,-90,0 even when moving to another province)
the orientation of each province ingame is 0,0,0 (might help you out later)
-- separated it so i could read it
local TargetPosition =
Vector3.new(mouseHit.Position.X,troop.PrimaryPart.Position.Y,mouseHit.Position.Z)
local LookAt = TargetPosition-troop.PrimaryPart.Position
CFrame = CFrame.new(TargetPosition,TargetPosition+LookAt)
-- I think this might work?
-- not sure what you have at the moment, but this looks right
it did start to sort of work, the troops were rotating in the correct axis (Y axis) and rotation was dynamic, but the problem now is that the troops are still not rotating to the position they’re moving to (which is a province)
it did move all the way to the province without any problems, but it did not rotate to the direction it was moving to fully (rotation still worked fine though)
yeah im not using orientation, but thanks
I tried getting the difference between the current troop position and the goal and adding it to the goal
so I dunno how it got 90degrees off its like it went halfway