ApplyImpulse wont work

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?

1 Like

What does your player variable contain? It should likely be the HumanoidRootPart. Also, RunService.Heartbeat might be the wrong place to put that call to ApplyImpulse. It may need to happen before the physics step, so possibly on RunService.Stepped.

I’d have to try it out to be sure though.

1 Like

image
This is my player variable.
The player controls a cannon which should be able to walk and turn and shoot (shooting and turning is done)

The cannon is the player and the small block you see in front of it is where the cannonballs starts from
image

1 Like

Ah, ok, I missed the part about it being a custom character. Then it’s likely the timing of the call, as Heartbeat happens after the physics update and Stepped happens before.

So I just have to change it to Stepped?

I’d give that a try. I know I’ve had problems with physics and timing of updates in the past.

Still nothing when I use WASD, The player object is unanchored, and something weird is with my turning apparently.

Also whenever I run the code on the server manually it works,

1 Like