How do I make something point in the direction it is moving in

Hi!
Lately I have been trying to get a part to move in the direction its heading, using things like taking two positions and orienting it so that it’s ends are connected with the positions, and taking its velocity and trying to turn it into some rotation value, but I wasn’t able to. Any ideas on how to do this? I feel like I have seen other games that have made this but idk. It is a simple part, its size is 0.5, 0.5, 9 and I nearly got it to work, using this code, but it was too chaotic and got launched into nowhere:

local part = script.Parent

while true do
	local pos1 = part.Position
	task.wait(.1)
	local pos2 = part.Position
	-- Set the position of the part
	part.Position = (pos1 + pos2) / 2
	-- Set the orientation of the part
	part.CFrame = CFrame.lookAt(part.Position, pos2)
end

Thanks for responding.

1 Like

You can always do something like this:

Part.CFrame = Part.CFrame + Part.CFrame.LookVector * 10

(Change the number for the amount of studs the part moves, hope this helps!)

You’re moving these parts using physics right?

If you are, you can just add .AssemblyLinearVelocity to get a predicted position.

Code:

local part = script.Parent

while task.wait() do
	--//Check if the part's speed is over 5 studs a second (if it's not, we will experience glitches when setting the CFrame)
	if part.AssemblyLinearVelocity.Magnitude > 5 then
		local currentPos = part.Position
		local predictedPos = currentPos + part.AssemblyLinearVelocity
		part.CFrame = CFrame.lookAt(currentPos, predictedPos)
	end
end

Yes, I am using physics to move the parts.

1 Like

Tried, and that is not what I want to accomplish. I want to use the roblox physics and then orient it based on something like position changes or velocity.

Just tried it, and when I ran it the part completely disappeared. Also, I’m gonna go to bed.

I’ve edited my script a bit.

1 Like

Okay, I will try it after I finish school.


here is a video showing what happened. And the part just goes flying after going to a resting point. But it does get closer to the desired affect. It points in the direction it is going in which is nice.

What type of terrain will this part be going through?

It is going to be going through flat terrain. Part of another game I have been working on but this is the test place for things I make.

Just find the proper axis.

part.CFrame *= CFrame.new(0, 0, 3) -- Change this to whatever axis the part is facing.

If you need help figuring out what the proper axis is, just click “Edit Pivot” and see what arrows are accurately displaying the direction the part is facing. Red arrow = X axis, Green arrow = Y axis, and Blue arrow = Z axis.

I am not trying to forcefully move the part at all, I am trying to use the physics and then orient it based off of its velocity.

Try increasing the check to 10 studs/s.

Code:

local part = script.Parent

while task.wait() do
	-- Check if the part's speed is over 10 studs a second
	-- If it's not, don't set the CFrame because glitches will happen
	if part.AssemblyLinearVelocity.Magnitude > 10 then
		local currentPos = part.Position
		local predictedPos = currentPos + part.AssemblyLinearVelocity
		part.CFrame = CFrame.lookAt(currentPos, predictedPos)
	end
end
1 Like

I have actually been messing around with it, and I have set it to 30, which is the best amount I was able to find because 25 was still too bouncy and 35 just stopped doing it too soon but 30 was the best. I still think that it could use some improvement though to do something with the bouncing.

So, I think that I should mark yours as the solution and I will keep working on it to improve it. Adding the thing to make it not as bouncy worked pretty well, and I will go off of that.

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