Issue with my CFrame

So no errors, but I can’t seem to get my part here to tween and face the other part. Some issue with my CFrame.fromEulerAnglesXYZ();
I’m just trying to rotate the Y axis so that it simply turns and faces a part on a flat plane essentially. But while it does rotate it doesn’t rotate and face my part. What am I doing wrong here? Thank you.

wait(3)

local tweenService = game:GetService("TweenService")

local part1 = script.Parent

local part2 = game.Workspace.FacePart -- The part that part1 will face

local rotation = CFrame.lookAt(part1.Part.Position, part2.Position)

local x,y,z = rotation:toEulerAnglesXYZ()

local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(y), 0)

local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing

local tween = tweenService:Create(part1.Part, info, {CFrame = goal})

tween:Play()
local tweenService = game:GetService("TweenService")

local part1 = script.Parent

local part2 = game.Workspace.FacePart -- The part that part1 will face

local rotation = CFrame.lookAt(part1.Part.Position, part2.Position)

local x,y,z = rotation:toEulerAnglesXYZ()

local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(y), 0) * CFrame.Angles(0, math.rad(180), 0)

local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing

local tween = tweenService:Create(part1.Part, info, {CFrame = goal})

tween:Play()

(this plugin try to fix it)

This script just makes it continue rotating in circles.

https://gyazo.com/44254b1647616e9033b91911ed1ecd4d

Anyone have any ideas to fix this here?

? anyone
////////////////////////////////////////

Still not clear on what you are wanting to achieve, and what is going wrong.

I put your script into a workspace, and one part moved to another part.

I watched the vid, but I didn’t see anything ‘keep rotating’

So maybe a more detailed explanation would help

https://gyazo.com/44254b1647616e9033b91911ed1ecd4d

It’s not facing the target properly

so the entire tank is supposed to be facing the target (block) as it moves… where is it moving, towards the block?

Yes sir!

Right now it seems to only rotate until it’s on it’s side when it reaches the destination.

I think that is because the model is not oriented the same as a roblox part.
such as, how parts have a front, back, top, bottom, left, right
I think you tank mesh is rotated incorrectly by 90 degrees on the Y axis.

You could put it in a ‘model’ and weld it to an invisible ‘part’ , or just re-import the mesh but rotate it 90 on the y first, or you can modify your code to calculate the rotation instead of the cframe.lookvector, use the - or + cframe.rightvector

Im already welding all mesh parts to a regular part. And moving that invisibel part :confused:

Top: https://gyazo.com/d06fadca247f349554426d3abbf5f063
Right: https://gyazo.com/f9198f5e386ca8466f72be8b079f7362
Left: https://gyazo.com/4695eef83b840fa1c266811b1f879412
Bottom: https://gyazo.com/e3dc05f2505436375e928284193365e4

All 4 of these examples I moved the destination brick.

I’ve tried rotating the main part all 4 ways, and no matter what I do it wont face the destination part properly.

Heck you can test this for yourself. This script has to be wrong. I literally just made a part in the workspace (1 single part) and if I move the part or the destination part. Every time the part faces a different angle.

wait(3)

local tweenService = game:GetService("TweenService")

local part1 = script.Parent

local part2 = game.Workspace.FacePart -- The part that part1 will face

local rotation = CFrame.lookAt(part1.Part.Position, part2.Position)

local x,y,z = rotation:toEulerAnglesXYZ()

local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, math.deg(y), 0)

local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing

local tween = tweenService:Create(part1.Part, info, {CFrame = goal})

tween:Play()

Something seems wrong about this, maybe replace part2.CFrame with CFrame.new(part1.Part.Position) or CFrame.new(part2.Position)?

Using Part1 position wouldn’t make any sense? It’s a part of the model.

And no, doing that changed nothing.

Sorry, I had to sleep.
Looking at it fresh this morning, I think you need to not use

local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, math.deg(y), 0)

but instead use

local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, y, 0)

TLDR, the fix was these two line…
original

local x,y,z = rotation:toEulerAnglesXYZ()
local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, math.deg(y), 0)

fixed

local x,y,z = rotation:toEulerAnglesYXZ()
local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, y, 0)

3Ducks.rbxl (43.8 KB)

Ok, I put 3 ducks, and have them try to get to the ‘pond’
I used a cylinder and rotated it to see if this would prove to be an issue.

image

The Pink Duck uses your code, with the fix.
image

wait(3)
local tweenService = game:GetService("TweenService")
local part1 = script.Parent
local part2 = game.Workspace.FacePart -- The part that part1 will face
local rotation = CFrame.lookAt(part1.Part.Position, part2.Position)
local x,y,z = rotation:toEulerAnglesYXZ()
local goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, y, 0)
local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing
local tween = tweenService:Create(part1.Part, info, {CFrame = goal})
tween:Play()

However the rotation ends up wrong.

The Green Duck uses the fixed code, but slightly different in calculating the goal
image

local goal = CFrame.fromEulerAnglesXYZ(0, y ,0) + part2.CFrame.Position

this seems to not have the orientation problems associated with the end goal being rotated.

The Yellow Duck is the same as the green duck, but I experimented with adding a Y offset
image

local goal = CFrame.fromEulerAnglesXYZ(0, y ,0) + part2.CFrame.Position+Vector3.new(0,1,0)

This also seemed to work well.

Hope this helps you.

1 Like

you already have your rotational value. instead of goal being

goal = part2.CFrame * CFrame.fromEulerAnglesXYZ(0, math.rad(y), 0)

you should add another set of X,Y, and Z to account for its current position and add the rotation u calculated onto it.

local x, y, z = rotation:toEulerAnglesXYZ()
local x2, y2, z2 = part2.CFrame:toEulerAnglesXYZ()

local goal = CFrame.new(part2.Position) * CFrame.fromEulerAnglesXYZ(math.rad(x2), math.rad(y), math.rad(z2))

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