Troops won't rotate by the Y axis to look towards a province

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)

any help would be appreciated

1 Like

Just update the CFrame value to look at the target

CFrame = CFrame.new(Vector3.new(x,troop.PrimaryPart.Position.Y,z),Vector3.new(Target.X,troop.PrimaryPart.Position.Y,Target.Z))

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)

Oh I dont understand you should just be able to set a Position and a look at position in the cframe

can i send a video of how the troop reacts when it moves with the script you gave me? (also the properties) and the line of code again

Yeah you can send a video, but I mostly dont get how its moving from a lookat value

here’s the video:
robloxapp-20231216-1022351.wmv (2.5 MB)
screenshot of one troop’s properties after moving:

the line of code that i changed to:

CFrame = CFrame.new(Vector3.new(mouseHit.Position.X, troop.PrimaryPart.Position.Y, mouseHit.Position.Z),Vector3.new(mouseHit.Position.X, troop.PrimaryPart.Position.Y, mouseHit.Position.Z))

Yeah I dont know whats making it do that
What does it do originally?
What does it do if you set y value of the lookat to 0?

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)

the look at is just a vector that the model points to
it doesnt rotate on x and z? it flipped the model onto its sides with x and z??

sorry about it, i meant it rotates on the x and y axis, not the z axis

here’s the properties after the troop finishes moving:


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)

Ooh! nice wdym not rotating to the position? did it move all the way there, but didnt rotate all the way?

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)

ohh so it rotated, but just slowly? did it end up at the right rotation at least? ( at the very very end )

it rotated at a fixed speed, the problem is that the troop did not rotate to the direction it was moving to

here’s a picture of it:
before moving:
Ekrano kopija 2023-12-16 111211

after moving:
Ekrano kopija 2023-12-16 111306

the troop orientation is also not 0,0,0 by default before it has moved at all (might help you out)

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

just figured out that i have to subtract some vector3 values after that to get an accurate rotation

thanks for the help anyway

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.