How to make a part move away from another part facing a certain direction?

You can write your topic however you want, but you need to answer these questions:

Hello, I am wondering how I can make a part move away from another part based on the way the parent is facing.

  1. What is the issue? Include screenshots / videos if possible!

My issue is that my test script is not working. I think I am doing velocity wrong.

wait(2)

local PART = script.Parent.Parent
local brick = script.Parent

brick.Position = PART.Position
brick.Anchored = false
brick.Veloctiy = Vector3.new(0, PART.CFrame.lookVector * (50), 0) --???
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried messing around with CFrame, asking friends, looking at other threads, etc but I can’t seem to get the last line to work. I have another solution to making this work but I want the part to move away from the parent based on where the part is facing. Not in any direction like so:

brick.Velocity = Vector3.new(math.random(-20,20), math.random(80,110), math.random(-80,-50))

Could I possibly get some help to clarify my problem?

3 Likes

well the current problem with your script would be Part.Cframe.LookVector * 50 as that would be a Vector3 I believe

now one solution to your problem would be

wait(2)

local part = script.Parent
local brick = script.Parent.Parent



-- this will be used to make sure your facing the part you want to avoid
local g = Instance.new("BodyGyro")
g.CFrame = CFrame.new(part.Position,brick.position)
g.MaxTorque = Vector3.new(500000000,500000000,500000000)
g.Parent = part

local speed = 50

wait(2)

---- body gyro I only call here in case the "brick" part moved.
g.CFrame = CFrame.new(part.Position,brick.Position)


part.Velocity = (part.CFrame + part.CFrame.LookVector * -speed).Position <-- this will make it go backward

edit actually what i just did with velocity would be used for bodyposition which would make it

wait(2)

local part = script.Parent
local brick = script.Parent.Parent



-- this will be used to make sure your facing the part you want to avoid
local g = Instance.new("BodyGyro")
g.CFrame = CFrame.new(part.Position,brick.position)
g.MaxTorque = Vector3.new(500000000,500000000,500000000)
g.Parent = part
local b = Instance.new("BodyPosition")
b.Position = part.Position
b.MaxForce = Vector3.new(500000000,500000000,500000000)
b.Parent = part
local speed = 50

wait(2)

---- body gyro I only call here in case the "brick" part moved.
g.CFrame = CFrame.new(part.Position,brick.Position)


b.Position = (part.CFrame + part.CFrame.LookVector * -speed).Position <-- this will make it go backward

2 Likes

if you wanted to use velocity i belive you would just

part.Velocity = part.CFrame.LookVector * -speed

along with orienting your self toward “brick”

The original line was:

.Velocity = Vector3.new(math.random(-20,20), math.random(80,110), math.random(-80,-50))

This works well:

part.Velocity = part.CFrame.LookVector * -speed

but I can only seem to get it to move if I put it in a loop. While in the original line, it is just ‘thrown’ away from the part so it is seemingly flying through the air by itself without the need of a loop or anything to keep it moving. Is it possible to do the same thing without the use of bodyMovers and loops?

there has to be some kind of way to where I can just do:

Vector3.new(math.random(-20,20), part.CFrame.LookVector * -speed, math.random(-80,-50))

or something and the part will always go out of the same correct side no matter which way the main brick is facing.

well you could go

part.Velocity = part.CFrame.LookVector * -speed + Vector3.new(math.random(-20,20), 0, math.random(-80,-50))

though I’m not really sure if that would work.

you should probably look through Explosion | Documentation - Roblox Creator Hub
because explosions may be another method to do it

sorry its taking so long to reply, I have school and can only look during breaks

you cold also remove the gyro right after it adjusts the parts orientation

wait(2)

local part = script.Parent
local brick = script.Parent.Parent



-- this will be used to make sure your facing the part you want to avoid
local g = Instance.new("BodyGyro")
g.CFrame = CFrame.new(part.Position,brick.position)
g.MaxTorque = Vector3.new(500000000,500000000,500000000)
g.Parent = part
g:Destroy()
local speed = 50

wait(2)

part.Velocity = part.CFrame.LookVector * -speed + Vector3.new(math.random(-20,20), 0, math.random(-80,-50))

if this would work idk as I don’t have time to test it

no good. It keeps moving in the same direction

you have just to see the orientation of the part

part = script.Parent
brick = script.Parent.Parent
part.CFrame = CFrame.new(part.Position,brick.Position)


local directionVector = (part.CFrame.p - part.CFrame.LookVector * .01).unit
local velocity = directionVector * -100
part.Velocity = velocity

actually there is a better way to do this


local power = 20
local height = 2

local part = script.Parent
local brick = script.Parent.Parent

part.CFrame = CFrame.new(part.Position,brick.Position)

part.ClickDetector.MouseClick:Connect(function()
	

	local directionVector = (part.Position - part.CFrame.LookVector * 1)+Vector3.new(0,height,0).unit
	local velocity = directionVector * -power
	part.Velocity = velocity
	
end)

watch the power though because it will make that height of 2 become a height of 40 or more and you’ll lose track of it as it launches out of the atmosphere

No good. I can’t get it to work.

hmm that was the last idea I had

also maybe try to



local power = 100
local height = 2
local distance = 5

local part = script.Parent
local brick = script.Parent.Parent

part.CFrame = CFrame.new(part.Position,brick.Position)

part.ClickDetector.MouseClick:Connect(function()


	local directionVector = (part.Position - part.CFrame.LookVector * distance).unit
	local velocity = directionVector * -power
	part.Velocity = velocity + Vector3.new(0,height,0)

end)

also I added a clickdetector into part so I could activate it

oh well It was worth trying, if you ever come back to it i would recomend trying to look through exposions api page and using them to fling object.

After many hours of thinking and research. I have found this to be the method I needed:

.Velocity = CFrame.Angles(0,math.rad(math.random(-45,45)),0) * script.Parent.CFrame.LookVector * math.random(70,120)

The item you want to throw will start the code. The item you want to face in any direction is the parent of the script.

2 Likes