Unit rotating 270 degrees instead of 90 degrees

I am making a similar moving system like game called Noobs In Combat. The problem i’am facing currently is that when having to turn only 90 degrees it turns 270 degrees for some reason. Here is the video explaining how it looks like.


How did i do it? At first gotta get the old orientation and then new orientation by making the player face towards X of the tile i’ve clicked using CFrame.LookAt(). After that i set the part’s orientation back to old orientation. Then i tween it to make the target the new cframe. IT always has to turn 90 degrees or 180 degrees, but for my thing it sometimes rotates 270 degrees. So how could i fix it?

2 Likes

If you’re using CFrame.Angles() then try actually converting the angles to radians:

CFrame *= CFrame.Angles(0,math.rad(90),0)

when I was making a tower defence game I had this problem this is how I fixed it. probably a better way.

local x,y,z = CFrame.new(node.Position, node_to_move_to.Position):ToOrientation()
x, y, z = deg(x), round(deg(y)), deg(z)

local poy = enemy.Orientation.Y

if poy < 0 and y == 180 then
    y = -180
elseif poy == 180 and y == -90 then
    y = 270
end

Interesting but i am using tweenservice to rotate it. As i said i use Cframe.LookAt to get the orientation it has to look at

Are you tweening the rotation directly or the cframe?

If you are tweening the rotation it might be the case that the enemies rotate from 0 to 270 because they turn -90, and rotation loops (which tween service doesn’t account for).

say your starting Y is -90 and you what to rotate to -180 roblox will not go down to -180 it will go 180 instead

1 Like

local cf2 = script.Parent.Parent.Parent.Parent.PrimaryPart.CFrame
local oldCFrame2 = script.Parent.Parent.Parent.Parent.PrimaryPart.Orientation
script.Parent.Parent.Parent.MainPart.CFrame = CFrame.lookAt(script.Parent.Parent.Parent.Parent.PrimaryPart.Position,Vector3.new(cf2.Position.X,cf2.Position.Y,v.Position.Z))

				local newCFrame2 = script.Parent.Parent.Parent.Parent.PrimaryPart.Orientation
				script.Parent.Parent.Parent.Parent.PrimaryPart.Orientation = oldCFrame2



				local Goal2 = {Orientation = newCFrame2}
				oldCFrame2 = nil
				newCFrame2 = nil
				local TweenAnimation2 = tweenService:Create(script.Parent.Parent.Parent.MainPart,TweenServiceInfo,Goal2)
				TweenAnimation2:Play()
				task.wait(0.3)
				TweenAnimation2:Destroy()
				TweenAnimation2 = nil

* List item

wait no something is wrong
I gotta send the code in another form

local cf2 = script.Parent.Parent.Parent.Parent.PrimaryPart.CFrame
					local oldCFrame2 = script.Parent.Parent.Parent.Parent.PrimaryPart.Orientation
					script.Parent.Parent.Parent.MainPart.CFrame = CFrame.lookAt(script.Parent.Parent.Parent.Parent.PrimaryPart.Position,Vector3.new(cf2.Position.X,cf2.Position.Y,v.Position.Z))

					local newCFrame2 = script.Parent.Parent.Parent.Parent.PrimaryPart.Orientation
					script.Parent.Parent.Parent.Parent.PrimaryPart.Orientation = oldCFrame2



					local Goal2 = {Orientation = newCFrame2}
					oldCFrame2 = nil
					newCFrame2 = nil
					local TweenAnimation2 = tweenService:Create(script.Parent.Parent.Parent.MainPart,TweenServiceInfo,Goal2)
					TweenAnimation2:Play()
					task.wait(0.3)
					TweenAnimation2:Destroy()
					TweenAnimation2 = nil

Alright here is the code ive made for orienting. THat v.Position.Z is the Z cordinates for the Tile you have clicked

Both oldCFrame2 and newCFrame2 here aren’t actually CFrames, they are the orientation which is a vector3 containing euler angles.

With that you can experience

Since tweenservice doesn’t know that -180 and 180 are the same angle, it will go from -90 to 180 the long way around.


I think simply changing .Orientation to .CFrame and Orientation = to CFrame = should fix it.

Like CFrame is position and orientation am i correct? I am trying to only rotate the thing.

Yes, but Orientation stores the rotation in a different format than CFrame, which causes weird issues like this. So you don’t really have any other way. Except if you were to create your own tween service, then it would be possible…

Another weird issue example:
Gimbal lock - Wikipedia Gimbal lock
(the plane can no longer be rotated in 3 directions because two of the circles (pink and blue) are stuck in the same direction because of the angle of the green circle)

So what should i do exacly? Like what should the target for tween be?

The pos with the desired rotation,
which you can combine like so:

position = ... -- vector3 position (without rotation)
rotation = ... -- cframe rotation (without position (0,0,0))

newPos = CFrame.new(position) * rotation -- order is important

Okay now The orientation thing is fixed but now its bugged and its teleporting when the first move is finished. Like its position becomes inbetween 2 tiles.

What do you mean exactly? Are you updating and using the latest position for both the old and new cframes?

like before it moves i just rotate the thing toward where its supposed to go and then it comes back to his old position. After that i do the tween. I use CFrame.LookAt to know where to rotate

and i feel like that is what causes the issue

Okay i managed to fix all of the problems. i really gotta thank you for helping me out.

1 Like