How to make a mouse offset?

To clarify, like 2 studs to the left of where the mouse position should be.
I have this little script that is erroring:

local hit = mouse.Hit.Position
local hit2 = hit * CFrame.new(Vector3.new(0,-2.75,1))

There error message:

Workspace.trueblockhead101.Shotgun.Barrel.LocalBulletShotgun:35: invalid argument #1 (CFrame expected, got Vector3)
1 Like

mouse.Hit.Position is a Vector3. You need to convert it to a CFrame:

local hit = mouse.Hit.Position
local hit2 = CFrame.new(hit) * CFrame.new(Vector3.new(0,-2.75,1))
1 Like

i get the error message unit is not a valid member of CFrame, I assume that’s because I originally had it as position. What should I change it to or should I just remove it?

Try this:

local hit = mouse.Hit.Position
local hit2 = CFrame.new(hit.X,hit.Y,hit.Z) * CFrame.new(0,-2.75,1)

I realized that using a Vector3 in a CFrame is useless, and it’s just better to get coordinates.

Ah ok. I think that part works but I have another script that creates a ray torwards the offset position were creating.

	local ray = Ray.new(tool.Barrel.CFrame.p, (hit - tool.Barrel.CFrame.p).unit*300)
	local ray2 = Ray.new(tool.Barrel.CFrame.p, (hit2 - tool.Barrel.CFrame.p).unit*300)

I assume since we changed it to cframe we’ll have to convert this also?

Yes, since they won’t be compatible types.

1 Like

So then I would take out the .unit part, and then what else would I change?

I’m still not too familiar with all the properties of cframes and what not so sorry If i’m being difficult.

Sadly same here.
But, I’ll think of something that can work.

1 Like

It might be from a typo.
When I look at Ray (developer.roblox.com), it says that it’s Ray.Unit
Edit: Actually, nevermind.

Maybe i should take away the .p since were using cframes?

Yeah, try that and let me know what happens.

1 Like

Ye nvm it didnt work. I just got this error message:invalid argument #1 (CFrame expected, got Vector3)

but i didnt get the .unit error

If anything, I’d probably try this if you didn’t do it already:

local ray = Ray.new(Vector3.new(tool.Barrel.CFrame.X, tool.Barrel.CFrame.Y, tool.Barrel.CFrame.Z), (hit - Vector3.new(tool.Barrel.CFrame.X, tool.Barrel.CFrame.Y, tool.Barrel.CFrame.Z)).unit*300)
local ray2 = Ray.new(Vector3.new(tool.Barrel.CFrame.X, tool.Barrel.CFrame.Y, tool.Barrel.CFrame.Z), (hit2 - Vector3.new(tool.Barrel.CFrame.X, tool.Barrel.CFrame.Y, tool.Barrel.CFrame.Z)).unit*300)

Edit: After searching around on Rays, I found that the constructor relies on Vector3.

Ya I already took out the .p and it didn’t work :confused:

It MIGHT be this (still searching for solutions):

local ray = Ray.new(tool.Barrel.Position, (hit - tool.Barrel.Position).unit*300)
local ray2 = Ray.new(tool.Barrel.Position, (Vector3.new(hit2.X, hit2.Y, hit2.Z) - tool.Barrel.Position).unit*300)
1 Like

Yes yes yes! It worked! Thanks for sticking with me and helping out!

Honestly I was just as confused as you were. I’m glad it worked!

1 Like