Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Vectors based on direction (lookVectors)?

Asked by
nilVector 812 Moderation Voter
8 years ago

I am trying to make a laser part come out out of another part (already made in Workspace) based on the positioning of it. It is not a tool, because I don't want the player's mouse to determine where it shoots. I want it to come out from the direction the part in Workspace is pointing to.

I was then led to lookVector after a while of researching. I tried using it like this:

--This is just the section where I create the part
--It's not important, but it's there in case you need it to study my script.
local part = Instance.new("Part", game.Workspace)
part.BrickColor = BrickColor.new("Really red")
part.FormFactor = "Custom"
part.Anchored = true
part.CanCollide = false
part.Material = "Plastic"
part.Transparency = 0.75

--Important Part (I need help on this section)
local base = game.Workspace.Base -- Base part
local distance = 30
part.Size = Vector3.new(0.02, 0.02, distance)
part.CFrame = CFrame.new(base.CFrame.p, base.CFrame.lookVector) * CFrame.new(0, 0, distance/2)

The problem is when I used lookVector like that, it points at a completely different direction and angle from where the base part is pointing to.

If I take out the lookVector part and only have one parameter in the CFrame.new() method, it only comes out in the positive z-direction regardless of where the base part is pointing.

I tried reading up more on lookVector, but the ROBLOX Wiki didn't have enough information for me to learn more about it. Any ideas on how I can do this?

1 answer

Log in to vote
2
Answered by 8 years ago

I think you don't mean part.CFrame = CFrame.new(base.CFrame.p, base.CFrame.lookVector) * CFrame.new(0, 0, distance/2). You probably mean part.CFrame = base.CFrame * CFrame.new(0, 0, distance/2).

Let me explain - CFrame.new(Vector3 position, Vector3 lookat) returns a CFrame at position, pointing towards lookat. This is a problem for you, because in that instance the lookVector is very close to 0,0,0

0
Thank you for your response. It helped my script to somewhat work, because it does do it based on the direction of the base part, but now it's always shooting out 90 degrees to the left of the base part. Do you know why it's doing that? nilVector 812 — 8y
0
The answer is simple. Your part isn't rotated how you thought it was. Try appending `* CFrame.new(Vector3.new(0,0,0),Vector3.new(1,0,0))` to the math and see how that rotates it. Fiddle about with it because I don't understand coordinates. User#6546 35 — 8y
0
Thanks! That worked! I'll accept your answer now. nilVector 812 — 8y
Ad

Answer this question