Effective ways to move parts/characters?

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!

2 Likes

I’d recommend looking into TweenService and LinearVelocity/BodyVelocity. They’re made to move parts around.

TweenService
BodyVelocity

1 Like

I tried BodyVelocity, and it doesn’t work:

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)

also doesn’t work. I don’t know why.

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.”

BodyVelocity is working now, but how can I make it move in the direction the part is facing?

You need to make the part face the mouse’s position then set the velocity’s multiplier to its LookVector multiplied.

LaserClone.CFrame = CFrame.new(LaserClone.Position, Mouse.Hit.Position)
BodyVelocity.Velocity = LaserClone.CFrame.LookVector * multiplier

Just replace multiplier with a number to amplify its speed and it will move it in the direction it’s currently facing.

2 Likes

It’s working, for the most part. It goes on the x/z axis correctly, but the y axis is stuck. How should I solve this?

What do you mean by the Y axis being stuck?

nevermind its all good, thanks, the issue is just its a bit buggy, like I click one area it goes the opposite, most of the time, its fine, though.

Be sure to mark the solution on the post in case anybody comes across the same problem as you. :+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.