How do I make a BodyPosition keep a constant speed? Is there some kind of formula where I can keep a constant speed depending on where I click?
It seems to slow the part when I click close and speed the part when I click far.
local mouse = game.Players.LocalPlayer:GetMouse()
local HS = game.Workspace:WaitForChild("Heavy Soldier")
local BG = HS:WaitForChild('Torso'):WaitForChild('BodyGyro')
local BP = HS:WaitForChild('Torso'):WaitForChild('BodyPosition')
mouse.Button1Down:Connect(function()
local mousPos = mouse.Hit.Position
if (HS.Torso.Position - mousPos).Magnitude <= 25 then
BP.Position = Vector3.new(mousPos.X, mousPos.Y, mousPos.Z)
BG.CFrame = CFrame.new(BG.Parent.Position, Vector3.new(mousPos.X, 1, mousPos.Z))
end
end)
Then do a check every frame or so for the distance. Once the distance is less than a certain threshold then remove the body velocity or set it to 0, 0, 0.
It’s an RTS, yes. The only similarities the two games have is the movement.
It seems to only move when my own character is within a certain distance of the unit… what?
local mouse = game.Players.LocalPlayer:GetMouse()
local HS = game.Workspace:WaitForChild("Heavy Soldier")
local BG = HS:WaitForChild('Torso'):WaitForChild('BodyGyro')
local BV = HS:WaitForChild('Torso'):WaitForChild('BodyVelocity')
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
--local BP = HS:WaitForChild('Torso'):WaitForChild('BodyPosition')
--BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local speed = 8
local function checkPoint(destination)
while true do
wait()
local distance = (HS.Torso.Position - destination).Magnitude
if distance <= 1 then
BV.Velocity = Vector3.new(0, 0, 0)
break
end
end
end
mouse.Button1Down:Connect(function()
local mousePos = mouse.Hit.Position
if (HS.Torso.Position - mousePos).Magnitude <= 100 then
print(mousePos, (HS.Torso.Position - mousePos).Magnitude)
--BP.Position = Vector3.new(mousPos.X, mousPos.Y, mousPos.Z)
BG.CFrame = CFrame.new(HS.Torso.Position, Vector3.new(mousePos.X, 1, mousePos.Z))
BV.Velocity = speed * CFrame.new(HS.Torso.Position, mousePos).LookVector
checkPoint(mousePos)
end
end)
No where is my own character included… But the unit moves only when I am near it.
I had this issue, it has to do with physics updating on the client. If you are producing parts on the client then it should be fine, but if you are cloning an unanchored part that the server controls in the workspace then this might happen. If you are cloning things, make sure you anchor before cloning so you don’t get this funny physics behaviour (then you can unanchor it).
In your case you are waiting for something on the server side (your troops I guess), no cloning involved. You would have to set NetworkOwnership so the server knows you are in control of those things. It’s not the same case as mine but the root cause is the same; unanchored parts controlled by the server have the server as the network owner. If the client tries to control those parts then you get funny behaviour.
If the server creates the armies, then automatically I believe the server is in charge of them. If you want to be sure you can set NetworkOwner to nil for the server to be in charge.
The RemoteEvent thing depends on how your game is set up. I would assume that if you generate armies on the server then they wouldn’t need a RemoteEvent other than for the player to communicate where to move them.
It just moves the physics calculations to the client. Without it, it takes the nearest player *sometimes (it’s undefined behaviour in my opinion) and the physics calculations are replicated to everyone else. When the server is in charge, it doesn’t have a player to refer to so it does the calculations for everyone. If you have it set locally then you control that part over the entire workspace. I think the reason for the distance thing is that if there’s physics that isn’t local that is “far away” then it shouldn’t matter to the player that much so it doesn’t play out until you get nearby.
It’s weird behaviour but there’s a few ways to deal with it like we discussed. Glad that it worked out
I’m not sure. I used to play The Conquerors 3 (and the older versions back in the day) but I’m not sure how it is implemented. There is some input lag last I remember so it is likely server sided.
No, the server owns all physics in the game. Both for security purposes and to avoid issues with sleeping parts where objects wont move when intended if theres no player near them.
Interesting. I assume you just moved the units server-sided via RemoteEvents?
How do I make it so the player doesn’t need to be near the unit to move it?
I dont know how to force physical parts to react to physics, ie not sleep, other than setting the ownership to the server. Something youll want to do anyway.