I am working on a custom character controller. I want to use ApplyImpulse to move the player, where the player is a cannon.
My problem is that ApplyImpulse won’t work it doesn’t move the player meanwhile, below is my client’s side of it, predicting their movement.
Client
RunService.RenderStepped:Connect(function(dt)
if newPlayer then
if not camera then -- CAMERA
camera = workspace.CurrentCamera
camera.CameraType = "Scriptable"
end
camera.CFrame = newPlayer.CFrame * CFrame.new(rotatedOffset)
----------------------------------------
--[[ Mouse controls
local mouseX, mouseY = mouse.X, mouse.Y
local deltaX, deltaY = mouseX - lastMouseX, mouseY - lastMouseY
if deltaX ~= 0 or deltaY ~= 0 then
local orientationChange = Vector3.new(0, deltaX * dt * rotSpeed, 0)
local newOrientation = (newPlayer.Orientation - orientationChange)
game.ReplicatedStorage.Events.Mouse:FireServer(newOrientation)
lastMouseX, lastMouseY = mouseX, mouseY
end
]]----------------------------------------
if left then
newPlayer.Orientation += Vector3.new(0,1,0) * dt * rotSpeed
elseif right then
newPlayer.Orientation += Vector3.new(0,-1,0) * dt * rotSpeed
end
-- Movement Controls
if w then
-- negative z forward
if not checkForWalls("front", dt) then
local impulse = newPlayer.CFrame.LookVector * -defaultImpulse -- example impulse vector
newPlayer:ApplyImpulse(impulse)
print(impulse)
end
end
if s then
--if not checkForWalls("back",dt) then
local impulse = Vector3.new(0, 0, defaultImpulse) -- example impulse vector
local localImpulse = newPlayer.CFrame:VectorToWorldSpace(impulse)
newPlayer:ApplyImpulse(localImpulse)
print(localImpulse)
--newPlayer.CFrame = newPlayer.CFrame * CFrame.new(Vector3.new(0,0,1) * moveSpeed * dt)
--end
end
if a then
--if not checkForWalls("left",dt) then
local impulse = Vector3.new(-defaultImpulse, 0, 0) -- example impulse vector
local localImpulse = newPlayer.CFrame:VectorToWorldSpace(impulse)
newPlayer:ApplyImpulse(localImpulse)
print(localImpulse)
--newPlayer.CFrame = newPlayer.CFrame * CFrame.new(Vector3.new(-1,0,0) * moveSpeed * dt)
--end
end
if d then
--if not checkForWalls("right",dt) then
local impulse = Vector3.new(defaultImpulse, 0, 0) -- example impulse vector
local localImpulse = newPlayer.CFrame:VectorToWorldSpace(impulse)
newPlayer:ApplyImpulse(localImpulse)
print(localImpulse)
--newPlayer.CFrame = newPlayer.CFrame * CFrame.new(Vector3.new(1,0,0) * moveSpeed * dt)
--end
end
--[[if os.clock() - lastUpdateTime >= UPDATE_INTERVAL then
local orientation = newPlayer.Orientation
if (orientation - lastOrientation).magnitude >= MIN_UPDATE_DISTANCE then
game.ReplicatedStorage.Events.Mouse:FireServer(orientation)
lastOrientation = orientation
lastUpdateTime = os.clock()
end
end]]--
end
end)
And then I have my server side script, which makes its multiplayer.
Server
RunService.Heartbeat:Connect(function(dt)
for i,v in pairs(players) do
if w then
-- negative z forward
--if not checkForWalls("front", dt) then
local impulse = v.player.CFrame.LookVector * -defaultImpulse -- example impulse vector
v.player:ApplyImpulse(impulse)
print(impulse)
--end
end
if s then
--if not checkForWalls("back",dt) then
local impulse = Vector3.new(0, 0, defaultImpulse) -- example impulse vector
local localImpulse = v.player.CFrame:VectorToWorldSpace(impulse)
v.player:ApplyImpulse(localImpulse)
print(localImpulse)
--newPlayer.CFrame = newPlayer.CFrame * CFrame.new(Vector3.new(0,0,1) * moveSpeed * dt)
--end
end
if a then
--if not checkForWalls("left",dt) then
local impulse = Vector3.new(-defaultImpulse, 0, 0) -- example impulse vector
local localImpulse = v.player.CFrame:VectorToWorldSpace(impulse)
v.player:ApplyImpulse(localImpulse)
print(localImpulse)
--newPlayer.CFrame = newPlayer.CFrame * CFrame.new(Vector3.new(-1,0,0) * moveSpeed * dt)
--end
end
if d then
--if not checkForWalls("right",dt) then
local impulse = Vector3.new(defaultImpulse, 0, 0) -- example impulse vector
local localImpulse = v.player.CFrame:VectorToWorldSpace(impulse)
v.player:ApplyImpulse(localImpulse)
print(localImpulse)
--newPlayer.CFrame = newPlayer.CFrame * CFrame.new(Vector3.new(1,0,0) * moveSpeed * dt)
--end
end
if v.left then
v.player.Orientation += Vector3.new(0,1,0) * dt * 50
elseif v.right then
v.player.Orientation += Vector3.new(0,-1,0) * dt * 50
end
---------------------------------------
-- Extra Parts Movement
v.player.SpawnBall.CFrame = v.player.CFrame * CFrame.new(Vector3.new(0,1,-5.5))
v.player.EndBall.CFrame = v.player.SpawnBall.CFrame * CFrame.new(Vector3.new(0,0,-111))
--v.player.SpawnBall.Orientation = Vector3.new(55, 0, 0)
end
end)
I want it to move all 4 ways using a way that interacts with collisions, any idea on why this happens?