Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make an object point toward its path?

Hello! I am currently scripting a football projectile using EgoMoose's open source code on projectiles. I am having trouble making the football point toward the correct direction while it travels in a parabola. This code currently makes the football point toward the target, however, the problem with this is that the football points straight up as it travels upward and then it points horizontally as it hits the target. What function would allow the football to point in the correct direction as it goes upward and downward?

local t = 1;
local mouse = game.Players.LocalPlayer:GetMouse();
local hrp = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart");
local football = script.Parent:WaitForChild("Handle");

mouse.Button1Down:Connect(function()
	local g = Vector3.new(0, -game.Workspace.Gravity, 0);
	local x0 = hrp.CFrame * Vector3.new(0, 2, -2)
	local target = workspace.Part
	
	-- calculate the v0 needed to reach mouse.Hit.p
	local v0 = (target.Position - x0 - 0.5*g*t*t)/t;
	
	-- have the ball travel that path
	local c = football:Clone();
	c.Velocity = v0;
	c.CFrame = CFrame.new(x0)
	c.CanCollide = true;
	c.Parent = game.Workspace;
end)

A visual of the problem: https://gyazo.com/89b011e1574ccbd641a7fbd0299cea4d

1 answer

Derive the rotation by subtracting the positional value CFrame.new(originalVector3, targetVector3), then isolate from the base value's position (which you already have at CFrame.new(x0)) and multiply that by the aforementioned CFrame's rotation.

..or just use a BodyGyro. Pick your poison.

2 comments

0

I am a bit confused as to how I would subtract the positional value and isolate the base value

0

Instead of using Velocity, you'd need to switch to CFrame so you can get the next point. Then you can use something similar to: local Whole = CFrame.new(a, b); local Rotation = Whole - Rotation.Position; local Position = Whole.Position. A should be the part's original position and B should be the next step in the bezier iteration.

This discussion is preserved as it was. New answers, comments, and votes are closed.