Help with rotation/orientation of a projectile

I have a working projectile however I’m not sure how I would change the orientation of it so it would shoot at the right angle depending on where the ray hit.

Here’s a video of the issue

local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRP = char:WaitForChild("HumanoidRootPart")

local RS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

local debounce = false
local CD = 0

local tpRemote = RS.Remotes.Teleport

local mouse = plr:GetMouse()

local KEY = Enum.KeyCode.V

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}


local function throwKunai()

	local rayOrigin = humRP.Position
	local rayDirection = mouse.Hit.Position

	local raycastResult = game.Workspace:Raycast(rayOrigin, rayDirection - rayOrigin * 1.01, raycastParams)
	
	if raycastResult == nil then print("nil")
		return
	end

	local kunai = RS.FX.RaijinKunai:Clone()
	kunai.Parent = workspace.Map.Ignore
	kunai.Position = humRP.Position
	kunai.CanCollide = false
	kunai.Anchored = true
	
	local distance = (rayDirection - rayOrigin).Magnitude
	local speed = 150
	
	local tweenTime = distance / speed
	
	local objHit = raycastResult.Instance
	
	local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	
	local kunaiDirection = {Position = raycastResult.Position}

	local tweenKunai = TS:Create(kunai, tweenInfo, kunaiDirection)
	
	tweenKunai:Play()
	
	print(objHit)
	print("Pressed V")
	
end

UIS.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if inp.KeyCode == KEY and not debounce then
		debounce = true
		throwKunai()
		tpRemote:FireServer(mouse.Hit.Position)
		task.wait(CD)
		debounce = false
	end
end)


1 Like

You should be tweening the CFrame, not the Position. CFrame is the standard way of changing spatial information for parts as it integrates both position and orientation.

Try changing this line to the following:

local kunaiDirection = {CFrame = CFrame.lookAlong(raycastResult.Position, rayDirection)}

And to explain, CFrame.lookAlong creates a CFrame such that it is located at the first parameter and “looks” along a ray projecting from it defined by the second parameter. It is similar to .lookAt.

If it is orientating the wrong way, you can fix this by offsetting the CFrame by some angle.

{CFrame = CFrame.lookAlong( ... ) * CFrame.Angles( ... )}
2 Likes

It didn’t exactly work as you can see here but I appreciate the help nonetheless! I’ll keep messing around with the angles to see if that fixes it at all.

You can use CFrame.LookAt instead of position:

-- Instead of:
--local kunaiDirection = {Position = raycastResult.Position}
--local tweenKunai = TS:Create(kunai, tweenInfo, kunaiDirection)

local kunaiPosition = kunai.Position
-- Look at creates a CFrame that starts at a first position and points towards a second
-- To flip the direction, multiply the CFrame.lookAt result by CFrame.Angles(0, math.rad(180), 0)
local kunaiGoal = {CFrame = CFrame.lookAt(kunaiPosition, raycastResult.Position)}
local tweenKunai = TS:Create(kunai, tweenInfo, kunaiGoal)

This makes a CFrame that starts at position kunaiPosition and points towards raycastResult.Position.

1 Like

Thanks so much! this works however would I have to create another tween to actually send the projectile to the rayCastResult Position because this only tweens the angle, or is there another way to do it in the same tween.

NVM, I’m an idiot, I just did the CFrame without the tween and that fixed it :sob:, thanks again though!

1 Like

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