Adjusting CFrame Y while keeping every other value of the CFrame?

So I’m making a tower defense game, and I have made a remote event to place towers.

Before you tell me to do the technique I’m already doing, here is my code:

-- 2.825 is the tower's height
-- Tower is the tower the player choosed
Tower.Joint.CFrame = CFrame.new(mousepos.X,mousepos.Y + 2.825,mousepos.Z)

Joint is the connection for the entire tower, I want it to keep the rotation of the players mouse so it faces where the player wants it to face, but this is the only way I can think of.

If you think something’s confusing in my post, lemme know because I’m tired and it’s like, 2 am, so I’m not very good at grammar right now lol

You can do CFrame * CFrame

local CF = Mouse.Hit

CF *= CFrame.new(0, DISTANCE_FROM_GROUND, 0)

image
Works perfectly, but can I ignore the Y orientation somehow, since it tilts weirdly like this due to the camera orientation?

How about just

local CF = Mouse.Hit
CF = CFrame.new(CF.Position) + Vector3.new(0, DISTANCE_FROM_GROUND, 0)

The orientation of the towers won’t really matter in Tower Defense games

1 Like

I want it to look nice, maybe I could somehow use the last values in CFrame?

If you want to keep only the Y part of the orientation, you can do this

local CF = Mouse.Hit
local XRotation, YRotation, ZRotation = CF:ToOrientation()

CF = CFrame.new(CF.Position) * CFrame.Angles(0, YRotation, 0)

You only want to keep the YRotation, the X and Z ones are the reason it looked weird. In the code above, it’s getting the rotation of the CFrame, then making a new CFrame at the old one’s location, and then rotating it only on the Y axis.

This is the right/the simplest/the nicest looking way to do it iirc. If you add a Vector3 to a CFrame, then only its position is changed, its orientation is not.
(edit: replace CFrame.new(CF.Position) with CF)

If you really want to do do literally what’s in the post title, you’d get the CFrame’s components, modify only Y and turn it back into a CFrame.

local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:GetComponents ( )
cf = CFrame.new(x, y + 5, z, R00, R01, R02, R10, R11, R12, R20, R21, R22)
2 Likes

Can’t you do

CF *= CFrame.new(0, 5, 0)

?

I realized I had misread your post earlier. This is how you do it.

CF += Vector3.new(0, 5, 0)

The original CFrame’s orientation is retained, but it goes up 5 studs in world space.

The reason why this

CF *= CFrame.new(0, 5, 0)

isn’t adequate because it goes up 5 studs in local space. If CF is upside down, then it’ll go down.

1 Like