Hello!
Currently I’m creating a star wars type of game with a main focus on vehicles, for practice. I was messing around with AssemblyLinearVelocity to create an X Wing, but I’m facing an issue.
The X Wing works fine but when i get close to the border of the baseplate it just stops in mid air. I noticed that when I somehow get the vehicle past the baseplate border, the ship works as intended.
What i have tried to solve this problem is disabling CanCollide on all parts of the model and I rewrote a part of my script, with the same results as before.
Local script
-- // Variables \\ --
-- Services
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local vehicleEvents = replicatedStorage:WaitForChild("VehicleEvents")
local playerInVehicleEvent = vehicleEvents:WaitForChild("PlayerInVehicleRemote")
local fireEvent = vehicleEvents:WaitForChild("FireRemote")
-- Player stuff
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local playerMouse = player:GetMouse()
local playerGui = player:WaitForChild("PlayerGui")
local screenGui = playerGui:WaitForChild("ScreenGui")
local camera = workspace.CurrentCamera
```lua
-- Vehicle stuff
local vehicle = workspace:WaitForChild("XWing")
local vehiclePrimaryPart = vehicle:WaitForChild("MainBody")
local vehicleCamera = vehicle:WaitForChild("PlayerCamPart")
local vehicleSettings = require(vehicle.VehicleSettingsModuleScript)
local playerInVehicle = false
local lastMouseHit = playerMouse.Hit
local mouseMoveDebounce = -math.huge
local newCFrame, newAngleCFrame, asin = CFrame.new, CFrame.Angles, math.asin
-- // Functions \\ --
-- This function sets the velocity to the vehicle
local function setAssemblyLinearVelocity()
vehiclePrimaryPart.AssemblyLinearVelocity = vehiclePrimaryPart.CFrame.LookVector * vehicleSettings.vehicleMaxSpeed
end
-- // Events \\ --
-- When the button gets clicked
screenGui.EnterBtn.MouseButton1Click:Connect(function()
playerInVehicle = true
playerInVehicleEvent:FireServer()
camera.CameraType = Enum.CameraType.Scriptable
end)
-- When the player moves the mouse
playerMouse.Move:Connect(function()
if not playerInVehicle then return end
local osClock = os.clock()
-- Debouncing
if osClock > mouseMoveDebounce then
-- Updates the last mouse position
lastMouseHit = playerMouse.Hit
mouseMoveDebounce = osClock + 0.1
end
end)
-- When player uses keyboard/mouse/controller
userInputService.InputBegan:Connect(function(input)
if not playerInVehicle then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
fireEvent:FireServer(lastMouseHit.Position)
script.Laser:Play()
end
end)
-- Fires every frame after physics simulation
runService.Heartbeat:Connect(function()
if not playerInVehicle then return end
local direction = vehiclePrimaryPart.CFrame:ToObjectSpace(lastMouseHit).LookVector * Vector3.new(0.02, 0.02, 0.02)
setAssemblyLinearVelocity()
--[[ The line below rotates the vehicle. We just arcsin to calculate
the angle from the direction and apply it to the vehicle ]]--
vehiclePrimaryPart.CFrame *= newAngleCFrame(0, -asin(direction.X), 0) * newAngleCFrame(asin(direction.Y), 0, 0)
camera.CFrame = vehicleCamera.CFrame
end)
Server script
-- // Variables \\ --
local replicatedStorage = game:GetService("ReplicatedStorage")
-- Events
local vehicleEvents = replicatedStorage:WaitForChild("VehicleEvents")
local playerInVehicleEvent = vehicleEvents:WaitForChild("PlayerInVehicleRemote")
local fireEvent = vehicleEvents:WaitForChild("FireRemote")
-- Vehicle stuff
local vehicle = workspace:WaitForChild("XWing")
local vehiclePrimaryPart = vehicle:WaitForChild("MainBody")
local vehicleWeapons = vehicle:WaitForChild("VehicleWeapons")
local vehicleSettings = require(vehicle.VehicleSettingsModuleScript)
-- Fastcast stuff
local fastCast = require(replicatedStorage:WaitForChild("FastCastRedux"))
local caster = fastCast.new()
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {vehicle}
local castBehavior = fastCast.newBehavior()
castBehavior.RaycastParams = raycastParams
castBehavior.AutoIgnoreContainer = false
--fastCast.VisualizeCasts = true
-- // Functions \\ --
--[[local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local enemy = hit:FindFirstAnchestor("XWing")
if enemy then
end
end]]--
-- // Events \\ --
-- When the player just entered the vehicle
playerInVehicleEvent.OnServerEvent:Connect(function(player)
vehiclePrimaryPart.Anchored = false
vehiclePrimaryPart:SetNetworkOwner(player)
end)
-- When the player wants to shoot
fireEvent.OnServerEvent:Connect(function(player, mousePos)
for i, machineGun in ipairs(vehicleWeapons:GetChildren()) do
local firePoint = machineGun.FirePoint.WorldPosition
local direction = (mousePos - firePoint).Unit
caster:Fire(firePoint, direction, vehicleSettings.shotSpeed, castBehavior)
end
end)
--caster.RayHit:Connect(onRayHit)
Any help is appreciated and, if you have suggestions just leave them below!