i made this as a testscript but now im stuck with this, unsure what to add (the script only allows the unit to move 500 studs sideways upon clicking and that’s pretty much it)
my aim is for the studunit to go where my mouse hits on the map
and no i don’t know how to use PathfindingService, so you’re welcome to briefly introduce me to the service if it does require it to work
local clickdetector = script.Parent.ClickDetector
local studunit = script.Parent
local distance = 100
local rotspeed = 20
clickdetector.MouseClick:Connect(function()
script.Parent.Anchored = true
for i = 0, distance do
studunit.CFrame = studunit.CFrame + Vector3.new(-1, 0, 0)
studunit.CFrame = studunit.CFrame * CFrame.Angles(0, math.rad(rotspeed), 0)
wait()
end
end)
please help
visual demo
I would recommend using PathfindingService so your units don’t get stuck walking into a corner, as that would be rather annoying.
Since it seems you are just using a part for the unit and not a player character, you will need to use the TweenService as well to move the part smoothly. The main issue wit hdoing it like this is that the clients might view it as laggy, in which case you’ll need to use RemoteEvents to tell each client to tween the object themselves.
For using the PathfindingService, there is a great example on the docs for it:
The only thing I would recommend is removing the Costs parameter from the CreatePath function as you probably don’t care what the units are walking on now.
For moving the parts, I’ll assume you are going to do it on the server first, but it is easy to change it to the client. You need to calculate the distance from the current position to the next waypoint position, which should just be this:
local distance = math.abs(studunit.Position.Magnitude - waypoint.Position.Magnitude)
Then, from this, you can calculate the amount of time the movement will take with:
local movementTime = distance / Studs_Per_Second
Use this movementTime for the duration of the tween.
Finally, to make sure it rotates, the final CFrame target should be something like
CFrame.new(newPosition) * CFrame.lookAt(studunit.Position, newPosition).Rotation
i request for a full stylized script to replace the one above
idk really, but now refuses to budge for sure
--local runservice = game:GetService("RunService")
--local players = game:GetService("Players")
--local player = game.Players.LocalPlayer
--local mouse = player:GetMouse()
local clickdetector = script.Parent.ClickDetector
local studunit = script.Parent
local distance = math.abs(studunit.Position.Magnitude - PathWaypoint.Position.Magnitude)
local studspersecond = 10
local movementTime = distance / studspersecond
local rotspeed = 20
local newPosition = studunit.Position + studunit.CFrame.LookVector * distance
--mouse.TargetFilter = studunit
--local function render()
-- local x, y, z = mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z
-- if mouse.Target then
-- studunit.CFrame = CFrame.new(x, y + studunit.Size.Y * 0.5, z)
-- end
--end
clickdetector.MouseClick:Connect(function()
script.Parent.Anchored = true
-- for i = 0, distance do
--studunit.CFrame = studunit.CFrame + Vector3.new(-1, 0, 0)
-- studunit.CFrame = studunit.CFrame * CFrame.Angles(0, math.rad(rotspeed), 0)
studunit.CFrame = CFrame.new(newPosition) * CFrame.lookAt(studunit.Position, newPosition).Rotation
wait()
-- end
end)
--runservice.RenderStepped:Connect(render)
script placement
Well, you never are actually using the pathfinding service in this script.
If you look at the example from the documentation, it is almost entirely what you want for figuring out where to go.
Sorry, I can’t make a whole script in Studio atm