Attempt to rotate seatweld on y Axis to where player is looking

This is a simple problem yet difficult for me to find anything on.

I’m trying to make the player’s SeatWeld rotate in correspondence with their camera’s Y axis/yaw. basically where the player is looking at on the Y Axis.

local seatOrientationC1 = seat.SeatWeld.C1

local newSeatOrientationC1 = CFrame.Angles(seat.SeatWeld.C1.Rotation.X, self.CurrentCamera.CFrame.LookVector.Y, seat.SeatWeld.C1.Rotation.Z) * seatOrientationC1

seat.SeatWeld.C1 = newSeatOrientationC1

I tried everything under the sun inregards to the camer’a properties, Cframe, Focus, LookVector “Rotation.Y” nothing really works

and for some reason Cframe.New or vector gives me a plethora of errors.

It looks like the entire rotation is being stacked every frame, but you’ll also need to find the difference in orientations between the seat and the camera rather than using the raw rotation of the camera.

This setup works reasonably well but is not perfect in that it’ll be fine if the seat stays fairly upright, but the angle snaps pretty hard when the seat flips over; unfortunately, I’m not well-versed enough in geometry to understand how to properly interpolate the angle when the seat flips, so maybe someone else can help. Anyway, here it is:

--This value needs to be cached and stored as an upvalue; it cannot be in your update loop or else the angle will stack.
local seat_weld = seat.SeatWeld
local seatOrientationC1 = seat_weld.C1

--Replace this loop with your actual update loop.
game:GetService("RunService").Heartbeat:Connect(function(): ()
	--"rotation" gets the difference in orientation between the seat and camera, then gets the second angle component (y-axis).
	local rotation = select(2, workspace.CurrentCamera.CFrame.Rotation:ToObjectSpace(seat.CFrame.Rotation):ToEulerAnglesYXZ())

	--When applying the angle, it is inverted if the seat is upside-down. This isn't perfect, but it mostly works.
	seat_weld.C1 = seatOrientationC1 * CFrame.Angles(0, 0, rotation * math.sign(seat.CFrame.UpVector.Y))
end)
2 Likes

thank you kindly, works just as i wanted. whats the possibility of this actually being replicated on server though? afaik the client’s camera can never be passed off to the server

The easiest way to get it to replicate is to apply it immediately on the local client and then send the camera’s CFrame to the server via a RemoteEvent and have the server calculate and apply the transformation, as well. This can, however, cause some stuttering on the local client depending on when the transformation is applied, especially if it is not every frame.

If you are getting stuttering, you can apply the transformation immediately on the local client and send the CFrame to the server, then have the server send that CFrame directly to the other clients and have them apply the transformation themselves. According to the server, the player will still be facing in the “default” direction, but everyone will see the intended direction, and it will still appear smooth to the local client.

1 Like

could rapid firing for event cause any issues or memory leaks? I am still exploring how i could do this, reading on renderStepped ahah seems very complex

It shouldn’t cause memory leaks, although firing too frequently may trigger anti-spam measures. I wouldn’t recommend firing the event directly from a RenderStepped handler since frame unlockers will cause it to run too frequently unless you explicitly tell it to only send an update once a certain amount of time has passed. Personally, I have not had problems using systems that send 60 updates per second; however, the more updates you send, the more network traffic will be dedicated to handling this system, so it may be prudent regardless to send fewer updates.

Sending fewer updates will make the appearance a bit more jagged to other players, but this can be combatted by lerping the previous update into the current update over the course of however long the delay between updates is.

1 Like

Thank you so much for the advise friend, I will look into it and experiment. :), I got the initial ‘version’ of it working using bindtorenderstep prior. but I will look into your method

1 Like

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