Help with LookVectors

I’m trying to make it so the Y axis of the trap will spawn in the same way as the players LookVector.

I’m unsure of how to convert the LookVector’s Y Axis into degrees. I tried doing some multiplication but that didn’t work.

trap.CFrame = CFrame.new(player.CFrame.X, pcframe - 2.7, player.CFrame.Z) * --CFrame.Angles(math.rad(0), math.rad(0), math.rad(0))
end)
2 Likes

Assuming trap and player are both BaseParts then you could do:

trap.CFrame = CFrame.LookAt(Vector3.new(trap.Position.X, 0, trap.Position.Z), Vector3.new(player.Position.X, 0, player.Position.Z))
local lookvector = (trap.CFrame.LookVector.X, humrp.CFrame.LookVector.Y, trap.CFrame.LookVector.Z)

edit:

local lookvector = Vector3.new(trap.CFrame.LookVector.X, humrp.CFrame.LookVector.Y, trap.CFrame.LookVector.Z)

I made this, try this out:

local Player = game.Players.PlayerAdded:Wait()
local Character = Player.Character or Player.CharacterAdded:Wait() 

	local PrimaryPart = Character.PrimaryPart
trap.CFrame = CFrame.new(PrimaryPart.Position - Vector3.new(0,2.7,0)) + PrimaryPart.CFrame.LookVector

EDIT:

Other one didn’t work, so I remade it:

local Player = game.Players.PlayerAdded:Wait()
local Character = Player.Character or Player.CharacterAdded:Wait() 

local PrimaryPart = Character.PrimaryPart
local LookVector = PrimaryPart.CFrame.LookVector

trap.CFrame = CFrame.new(PrimaryPart.Position - Vector3.new(0,2.7,0)) * 
	CFrame.lookAt(Vector3.new(0,0,0), Vector3.new(LookVector.X, 0, LookVector.Z))

The type solver errors this because this is not a vector. Wont work.

You cant use + on CFrames, you have to use *

If your talking about this part:

The look vector is not a CFrame, it is actually a vector, so you can use addition on it. Hence the name, look “vector”.

Vectors and CFrames can be added up.
CFrames and CFrames have to be multiplied.
Vectors and Vectors can be added up.

You can use math.asin to get Y-Axis angle from the LookVector Try this:


local lookVector = player.CFrame.LookVector
local yAngle = math.asin(lookVector.Y) -- Get the tilt in radians

trap.CFrame = CFrame.new(player.Position - Vector3.new(0, 2.7, 0)) * CFrame.Angles(yAngle, 0, 0)

This will make the trap align with the players vertical LookVector angle

How will this even work??

If the player doesn’t have a CFrame??

I tested your idea, yet it doesn’t seem to be working.

oh my bad I thought you were trying to add 2 CFrames I didn’t notice the .LookVector

1 Like

Other one didn’t work, so I remade it:

This works, but is very laggy. Is there any alternatives? (I’m making a trap that spawns at your position and the way you’re looking when you press a key, for some context)

trap.CFrame = CFrame.new(PrimaryPart.Position - Vector3.new(0,2.7,0)) * 
	CFrame.lookAt(Vector3.new(0,0,0), Vector3.new(LookVector.X, 0, LookVector.Z))

How is it laggy exactly?

Im working on an alternative though.

I’m sorry i’m not fully awake to talk but i still tried to make an answer.

oh mb, keep sleeping then lmao.

I got another version, test this one out:

local Player = game.Players.PlayerAdded:Wait()
local Character = Player.Character or Player.CharacterAdded:Wait() 

local PrimaryPart = Character.PrimaryPart
local LookVector = PrimaryPart.CFrame
local X, Y, Z = LookVector:ToEulerAnglesXYZ()

	trap.CFrame = CFrame.new(PrimaryPart.Position - Vector3.new(0,2.7,0)) * CFrame.Angles(X, Y, Z)

I accidentially had a while loop on it, whoops

this worked! thank you

1 Like

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