Unity weird snap with transform.lookat

dapcos

New Member
Oct 9, 2022
5
3
so i've been trying to fix the problem in the video for enough time to ask for outside help, why is it doing that? when i manually move the target it works fine but then when the rest of the code happens it does that weird snapping.
 

Tompte

Member
Dec 22, 2017
216
157
Hmm. It could be .

From what I can see, the code is slowly but surely attempting to point the breast straight up, or [0, 1, 0].

If you check the , there is a second parameter for passing an up-vector, and if omitted it defaults to [0, 1, 0]. The up-vector is just a vector describing which 3D-axis is "up" and it's necessary to disambiguate the math (when using Euler angles). However, if the direction you're pointing towards is equal or very close to the up-vector, you can run into what's called gimbal lock, which is when two reference axes are the same and thus you lose one degree of freedom in your rotation. For simplicity's sake, you can think of it like dividing by zero, for rotations.

The solution is to 1) never use LookAt() to point exactly up, or 2) pick an up-axis that is different from the direction you're looking towards (i.e. the two vectors should never be the same), or 3) use rotation instead, because it avoids gimbal lock completely.

I don't know if this is the problem you're having, but those two vectors being the same is a no-no, so it stands out to me as a possible cause. It could be that the breast is pointing upwards but its own rotation around its nipple is undefined, or becomes subject to precision errors.
 
Last edited:
  • Like
Reactions: dapcos

dapcos

New Member
Oct 9, 2022
5
3
Hmm. It could be .

From what I can see, the code is slowly but surely attempting to point the breast straight up, or [0, 1, 0].

If you check the , there is a second parameter for passing an up-vector, and if omitted it defaults to [0, 1, 0]. The up-vector is just a vector describing which 3D-axis is "up" and it's necessary to disambiguate the math (when using Euler angles). However, if the direction you're pointing towards is equal or very close to the up-vector, you can run into what's called gimbal lock, which is when two reference axes are the same and thus you lose one degree of freedom in your rotation. For simplicity's sake, you can think of it like dividing by zero, for rotations.

The solution is to 1) never use LookAt() to point exactly up, or 2) pick an up-axis that is different from the direction you're looking towards (i.e. the two vectors should never be the same), or 3) use rotation instead, because it avoids gimbal lock completely.

I don't know if this is the problem you're having, but those two vectors being the same is a no-no, so it stands out to me as a possible cause. It could be that the breast is pointing upwards but its own rotation around its nipple is undefined, or becomes subject to precision errors.
damn i just read about gimbal lock like 3 days ago, interesting to see a real world example of it happening, thank you for the help
 
  • Like
Reactions: Tompte