How can I add an Offset to a Vector3 while adjusting for the Player's Orientation?

  1. What do you want to achieve?
    I have a script that is meant to move an accessory using Handles for an RP game, similar to how it is handled in After The Flash 8. I have a part that is created in the SpecialMesh of an accessory in another script used as an Adornee for the Handle which is meant to move with the accessory as it’s offset is set.
    The code I have so far is listed below as part of a LocalScript. Later, I plan on making it so that the MouseButton1Up triggers it to reflect on the server as well.
local PartToMove = nil -- Set to Nil just in case!
local movetool = script.Parent -- This is the Handle

	local origin = nil -- Same for here as Local PartToMove!
	movetool.MouseDrag:Connect(function(a, b)
		if a.Value == 5 then
			PartToMove.Offset = Vector3.new(origin.X, origin.Y, -b) -- Changing the offset of the Accessory, works as intended, same goes for a lot of following lines
		elseif a.Value == 2 then
			PartToMove.Offset = Vector3.new(origin.X, origin.Y, b)
		elseif a.Value == 1 then
			PartToMove.Offset = Vector3.new(origin.X, b, origin.Z) 
		elseif a.Value == 4 then
			PartToMove.Offset = Vector3.new(origin.X, -b, origin.Z) 
		elseif a.Value == 0 then
			PartToMove.Offset = Vector3.new(b, origin.Y, origin.Z) 
		elseif a.Value == 3 then
			PartToMove.Offset = Vector3.new(-b, origin.Y, origin.Z)
		end
		PartToMove.Part.Position = PartToMove.Parent.Position + PartToMove.Offset -- Trying to move the part inside the SpecialMesh that the Handle has it's Adornee set to
	end)

	movetool.MouseButton1Up:Connect(function()
		origin = PartToMove.Offset
	end)

game.ReplicatedStorage.accessorySelect.Event:Connect(function(x) -- 'X' is the accessory I'm getting the SpecialMesh from
	PartToMove = x.Handle:FindFirstChildOfClass("SpecialMesh")
	origin = PartToMove.Offset
end)
  1. What is the issue?
    While looking in one specific direction has it work as intended, if I try and turn away from that direction however the location of the accessory and the part within the SpecialMesh seems to ‘desync’ and the Handle goes off in a completely different direction to the Accessory. Image demonstrates what I mean, the Handles should be moving with it.

  2. What solutions have you tried so far?
    As I come from an Unreal Engine background, my first thought was to try and figure out how i could “rotate the vector” of the PartToMove.Offset on line 19. I couldn’t find anything on this that worked on the dev forum nor anywhere else I’ve asked. I have consulted developer friends about this (who are, admittedly, only just slightly more experienced as I am) and gotten one to look through my code in Team Create and they have drawn blanks as well.

I’m not very good at Lua, as I’ve quite literally only started seriously considering making things in it and working on this project on the Game Dev side starting around last week, so try explain things in a manner that’s easy for me to rack my brain around as a Lua beginner but someone who isn’t fully new to programming.

Thanks!
(Sorry for poor wording by the way, I’m not the best at getting myself across.)

(post deleted by author)

Tried this just now
grafik
The Handle seems fixed to my head position and if I try move the accessory it seems to push me in the direction I try move it

Also, please if you’re going to give a response ripped from ChatGPT at least try make it not painfully obvious. I put in my exact question and it gave the exact same reply as you posted.

Thanks!

(post deleted by author)

multiply your vector components relative to the cframe and then add them together

CFrame.LookVector * z component +
CFrame.RightVector * x component +
CFrame.UpVector * y component

1 Like

I think you’re onto something here, but I couldn’t get this to work. This is how I attempted it.

		local PTMX = PartToMove.Offset.X -- Getting SpecialMesh in accessory's X, Y and Z offsets
		local PTMY = PartToMove.Offset.Y
		local PTMZ = PartToMove.Offset.Z
		PartToMove.Part.Position = PartToMove.Parent.Position + Vector3.new(PartToMove.Part.CFrame.RightVector * PTMX, PartToMove.Part.CFrame.UpVector * PTMY, PartToMove.Part.CFrame.LookVector * PTMZ) -- The math, as I interpreted it.

actually a better idea is toworldcframe

--using partToMove.CFrame as the origin and offset as the offset from that origin
partToMove.CFrame:ToWorldCFrame(offsetCFrame)
1 Like

Might have been slightly deceptive in how I named the variable as ‘PartToMove’ isn’t actually a Part. It’s a SpecialMesh. I don’t think I can get a CFrame from that. Oops!

use the part its inside of

or the model with :GetPivot()

1 Like

Not fully sure what you mean here, could you demonstrate or at least give a more thorough explanation? I hope this isn’t asking too much. Thanks!

There is an offset on the accessory itself. You may have to consider that.

Yes, I am considering that. I am referencing it several times in this code. The issue is I want to ‘rotate’ that offset so I can add it onto the PartToMove.Parent.Position and have the part inside the SpecialMesh move when I change the offset to match the Accessory’s location, not go off in other directions.

maybe;
PartToMove.Part.Position = PartToMove.Parent.CFrame:PointToWorldSpace(PartToMove.Offset)

1 Like

Eyy, whaddya know, it works! Appreciate it dude!
devil-may-cry-dante

1 Like