Python rotational copy issue

oops

I have a space game prototype in which the green arrow pictured points to the nearest planet, and the green text is supposed to be above it by copying its rotation (instead of having a parent child rotation). Except, occasionally something happens like in this screenshot that the text flips to the opposite side of the arrow. I know this is probably a simple code fix, but I can’t find a solution for it, and I can’t figure it out.

     ownRot = own.worldOrientation.to_euler()
     targetRot = dir.worldOrientation.to_euler()
     ownRot.z = targetRot.z
     own.worldOrientation = ownRot

Thanks

thats gimbal lock i think.

why copy only the Z? why not copy the whole orientation matrix?

it should be enough to just copy the orientation, euler is more for adding rotation to an object.
Also welcome to the .z issues (or xyz) use .col instead (0-x,1=y,2=z)

own.worldOrientation.col[2] = dir.worldOrientation.col[2]

also you can use .copy() behind it to copy it from the frame you are in instead of 1 frame later, like:

own.worldOrientation.col[2] = dir.worldOrientation.col[2].copy()

also check out orientation.lerp(other orientation, rate)

Cotaks, I tried the exact code that you suggested and it did not rotate when I pressed play.

I continued to look for instances of use of worlOrientation.col[] to see how I could correct it, and I found no record of it. Could it be that you misspelled it or misremembered the code?

check the console output for errors, typically you can debug code this way.

hmm i see, strange.

this works but on my end it’s not accurate.

own.worldOrientation = dir.worldOrientation.col[2].copy()

as @Daedalus_MDW said can’t you just copy all?

own.worldOrientation = dir.worldOrientation.copy()