Vector3 woes (I think)

First off, sorry if the solution is quite simple. I haven’t done any luau in about a year, so I am definitely not competent.

Anyway…
So I am working on a retractable sword (details are not important), and my method of making this happen is 2 tweens. The first one adjusts the size of the sword blade to be right and then the second tween one moves the blade to make up for the first tween making the blade grow in both directions. There is an issue here though, since I am using vector3 for the second tween (does not matter for the first one as far as I know), the blade only is in the correct place if i am facing in the correct axis. I tried using cframe for it because that seemed to be the correct solution, but I think I was wrong as that was flat out not working…

To be honest I have no idea if using 2 tweens is even right for this. Not knowing if I am even in the ballpark of “correct” is not fun, so that’s part of why I am asking here. Thank you in advance :slight_smile:

Here is a video of the issue, the blade moving is me pressing E (I need to have a retract, just ignore that part)

Lastly, the code (don’t roast me too hard):

local player = game.Players.LocalPlayer
local userInputService = game:GetService("UserInputService")
local tweenService = game:GetService("TweenService")
local contextActionService = game:GetService("ContextActionService")
local playTweensEvent = game.ReplicatedStorage:WaitForChild("PlayTweensEvent")

local blade = script.Parent.Blade
local handle = script.Parent.Handle

-- function to create the tweens
local function createTweens()
	local animTime = .15
	local easingStyle = Enum.EasingStyle.Linear
	local easingDirection = Enum.EasingDirection.InOut
	local tweenInfo = TweenInfo.new(animTime, easingStyle, easingDirection)

	-- Tween1: change blade size
	local goal1 = {Size = Vector3.new(4.5, 0.25, 0.25)}
	local tween1 = tweenService:Create(blade, tweenInfo, goal1)

	-- Tween2: move the blade
	local goal2 = {Position = handle.Position + Vector3.new(2.25, 0, 0)}
	local tween2 = tweenService:Create(blade, tweenInfo, goal2)

	return tween1, tween2
end

-- function to play the tweens
local function playTweens()
	local tween1, tween2 = createTweens()
	tween1:Play()
	tween2:Play()
end

-- detect the key
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end

	if input.KeyCode == Enum.KeyCode.E then
		playTweens()
	end
end)

Have you tried:

local goal2 = {CFrame = handle.CFrame * CFrame.new(2.25, 0, 0)}

?

2 Likes

Is the issue that its moving forward instead of to the side in the second tween? If so then you could try {Position = handle.Position + Vector3.new(0, 0, 2.25)}.

I would lerp/tween the offset of the weld connecting the blade while resizing it.

1 Like

Hey! Yes I did try using cframe in this same way, but it wasn’t working how I thought it would. I’ll try it again though, thank you for the reply

Hmm that seems like an elegant solution. I’ll definitely try that as well. Thanks!

1 Like

CFrame uses orientation and position so its a lot better than vector3 which only uses position.

1 Like

rather than tweening the blade itself, you should tween the c1 property of the weld (assuming the blade is part1)

1 Like

Okay this definitely fixed it, so thanks for getting me to try cframe again. The issue is the weld now…time to figure that out.

Also thank you @TomPlays63

1 Like

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