# How to make BodyPosition constant?

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)
``````
2 Likes

Instead of using a BodyPosition, try using a BodyVelocity.

1 Like

Iâ€™ve tried using BodyVelocity but I canâ€™t figure out how to make it work with mouse.Hit.Position.

``````BodyVelocity.Velocity = _SPEED_ * CFrame.new(HS.Torso.Position, mousePos).LookVector
``````

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.

Are you making a game like The Conquerors?

1 Like

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.

Wouldnâ€™t the simple fix just use a RemoteEvent and move BodyVelocity and such to the server side?

1 Like

You could do that yes. It would be smoother on client side (no lag) but yeah you could just do it from the server to avoid NetworkOwnership headaches.

Itâ€™s also a good way to avoid exploiters flinging their armies around the map at other players.

1 Like

How would I set NetworkOwnership to the model serverside?
Wouldnâ€™t I need a RemoteEvent anyway in order to set it to the server?

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.

Ah! I set NetworkOwnership of the soldier model to the player, now it works.
What does it do?

1 Like

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

1 Like

Would a game like The Conquerors 3 be putting each teams soldiers as their own client network ownership?

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.

1 Like

Im the developer of TC3

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.

Yes I use remote events

1 Like