Hello, I am making a space shooter game. I want to move bullets and the player. I am trying to use BodyPosition. They both have a problem though. With the player, it only moves up and down, with the occasion of moving on the x/z axis for like 2 seconds every once in a while. With the bullets, they simply don’t move. This is my player code:
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Char = script.Parent
local Humanoid = Char:WaitForChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
local Mouse = Player:GetMouse()
local MousePosition = Mouse.Hit.Position
local BodyPosition = Instance.new("BodyPosition", HumanoidRootPart)
BodyPosition.MaxForce = Vector3.new(12000, 12000, 12000)
BodyPosition.D = 1000
while true do
task.wait()
MousePosition = Mouse.Hit.Position
BodyPosition.Position = MousePosition
end
Bullet code:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Char = script.Parent
local Mouse = Player:GetMouse()
local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}
local Mouse = Player:GetMouse()
local MousePosition = Mouse.Hit.Position
local DB = false
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
DB = true
local LaserClone = Laser:Clone()
local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]
local BodyPosition = Instance.new("BodyPosition", LaserClone.Root)
BodyPosition.Position = MousePosition
BodyPosition.MaxForce = Vector3.new(12000, 12000, 12000)
BodyPosition.D = 1000
LaserClone.Parent = Char:WaitForChild("Attacks")
LaserClone:MoveTo(SelectedShooter.Position)
MousePosition = Mouse.Hit.Position
LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))
task.wait()
DB = false
end
end)
I have tried looking through the documentation and the devforum, but all the devforum results I found didn’t really do what I wanted or really help. If they were what I wanted, they didn’t work for some reason. With the documentation, I couldn’t find anything that worked. If anyone has a solution, I’d greatly appreciate it. Thank you!
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Char = script.Parent
local Mouse = Player:GetMouse()
local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}
local Mouse = Player:GetMouse()
local MousePosition = Mouse.Hit.Position
local DB = false
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
DB = true
local LaserClone = Laser:Clone()
local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]
local BodyVelocity = Instance.new("BodyVelocity", LaserClone.Root)
BodyVelocity.Velocity = Vector3.new(1000,1000,1000)
BodyVelocity.MaxForce = Vector3.new(12000,12000,12000)
LaserClone.Parent = Char:WaitForChild("Attacks")
LaserClone:MoveTo(SelectedShooter.Position)
MousePosition = Mouse.Hit.Position
LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))
task.wait()
DB = false
end
end)
I tried tween:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local Player = Players.LocalPlayer
local Char = script.Parent
local Mouse = Player:GetMouse()
local Laser = ReplicatedStorage:WaitForChild("Assets"):WaitForChild("LaserProjectile")
local LaserShooters = {Char:WaitForChild("LLaserShooter"), Char:WaitForChild("RLaserShooter")}
local Mouse = Player:GetMouse()
local MousePosition = Mouse.Hit.Position
local DB = false
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Space then
DB = true
local LaserClone = Laser:Clone()
local SelectedShooter = LaserShooters[math.random(1, #LaserShooters)]
local tweenInfo = TweenInfo.new(
1,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
local tween = TweenService:Create(
LaserClone.Root,
tweenInfo,
{
CFrame = CFrame.new(MousePosition)
}
)
tween:Play()
LaserClone.Parent = Char:WaitForChild("Attacks")
LaserClone:MoveTo(SelectedShooter.Position)
MousePosition = Mouse.Hit.Position
LaserClone:PivotTo(CFrame.lookAt(LaserClone.Root.Position, MousePosition))
task.wait()
DB = false
end
end)
For the BodyVelocity, there may be 2 reasons. Either the part is Anchored or you forgot to change BodyVelocity’s P property that determines how fast it changes the velocity of the part affected by it.
As for the tween, it may be because you’re playing the tween before parenting it to “Attacks.”