Align car turret on inclined terrain

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to rotate the turret of my car toward the target while the turret still being align with the car.

  1. What is the issue? Include screenshots / videos if possible!

The turret align properly on flat terrain.
The turret is not align properly on inclined terrain.

Video of the problem: https://youtu.be/ZWuRbjdZE6o

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I have tried to look the forum for solution but I didnt found something that ended helping me.
Model of the car: https://create.roblox.com/store/asset/18800842920

The default orientation of RotationPart: 0, 0, 90
The default orientation of Chassis: 0, 90, 0

ShootVisualEvent.OnServerEvent:Connect(function(player, RotateToward)
		print("Testing")
		local RotationPart = Mod.MainBody_Mod
		local Chassis = Car.MainBody

		-- Step 1: Calculate the direction to the target
		local direction = (RotateToward - RotationPart.Position).unit
		local lookAt = CFrame.lookAt(RotationPart.Position, RotateToward)
		local _, yaw, _ = lookAt:ToEulerAnglesYXZ()

		-- Step 2: Get the car's current orientation
		local chassisCFrame = Chassis.CFrame
		local chassisPitch, chassisYaw, chassisRoll = chassisCFrame:ToEulerAnglesYXZ()

		-- Step 3: Create the final CFrame for the turret
		local finalCFrame = CFrame.new(RotationPart.Position) * CFrame.Angles(chassisPitch, yaw, chassisRoll + math.rad(90))

		-- Step 4: Apply the final CFrame to the turret
		RotationPart.CarLinker.Enabled = false
		RotationPart:PivotTo(finalCFrame)
		RotationPart.CarLinker.Enabled = true
	end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Update on the situation: The script listed above is still my best attempt yet. I really don’t know what else to adjust or try.

Hi frivsasasa,
You can try not sending the value of Y to the turret,
I suspect the inclination could be from that value telling the turrent to incline because the car is inclined, you can also try to make some of the x,y,z values to negative (-x) for example.
Just mess around a bit around these points.

Ok thank, I will try that. I will also mark it as the solution if it work.