# Orientation Calculation problems

1. What do you want to achieve? Im trying to make a script that only orientation the x and z axis of a part, and keep the y axis the same

2. What is the issue? I have no issues if my script i guess, right now i just make the script orientate the Z axis and not the X, and the reason of why that, its because i can’t find a way to make it.

``````local function adjustMarkerOrientation(marker, worldNormal)
local fixedForward = Vector3.new(0, 0, 1)
local fixedUp = Vector3.new(0, 1, 0)

local rightDirection = worldNormal:Cross(fixedForward).Unit
local newUpDirection = rightDirection:Cross(fixedForward).Unit
local newCFrame = CFrame.fromMatrix(marker.Position, rightDirection, newUpDirection)

marker.CFrame = newCFrame
end
``````

the variable worldNormal is the RayCast.Normal

Thanks to anyone that is helping me.

So my suggestion is:

1. Store the axis you want to lock
2. Perform the operations that will change orientation on all of the axis
3. Overwrite the Axis with the one you saved and locked, with the one you stored.

Now if there is some sort of weird math involved that is causing this not to look right, you can split it up into 2, 2d rotations and then treat them separately accordingly and only exit the X and Z, and then you can apply those changes in the order you want too.

I am pretty sure there is a built in function that will do it all in one go, and then you overwrite but I am not looking at my math library right now. But if not, you can do the orientations individually as 2d rotations.

I hope this helps.

1 Like

I aldery tried it it dosent work either ;( is this impossible maybe, btw i forgot to say but the function need a limiter, if x + z = 180 then y will automatic change.

Sounds like you should break them up and do them as 2d rotations, that way you can detect and make changes individually more specifically. It will require a few more maths to do but will allow you the control you want.

When in doubt, break everything down to primative math and got it working as small pieces, once you have it behaving exactly how you want, you can then look to see if there are any ways of reducing the processes needed for it or combing math features to do it or if there are any built in ways to do parts. Though if this action is not happening every frame, you are probably fine with it being less performant.

and how i do that, i dont know how to use 2D rotations well

Whispers:

"ask chat gpt, “Please explain and give examples of the math of how to rotate a 2d vector by an angle”

– Read then

“Past, using lua could you write a function that lets me pass in a vector 2 and a rotation and return a new vector 2 with the rotation applied?”

Then play around with it to see how it behaves and ensure that it is working like you want, and be curious ask more about it break it down and if you still don’t understand you can ask it to break it and explain it better and less complexly.

You will notice how to do this, while in this specific case I am not agienst copy and past for a math solver of this BUT I highly suggest you get it, spend one day understanding it ensuring that whatever code you use works reliable in the way you expand and have a general understand and chat GPT is GREAT for this. You aren’t asking it just to do all the work for you, you are getting general understanding having it show an example, and then you will transpose that code into your own code.

That being said, I am partial. I spent a good deal of time one month long ago manually recoding javascript math libraries over to lua because I knew they would be helpful and ensuring each one worked as expected.

I can’t tell you how the math for detecting 2, 2d circles intersecting is, but I understand what goes on just I cant remember the raw math for it, but because I understand the behavior, and the general idea of it, not only do I know when to use that kind of detection, but I know aht to expect from the formula. =)