I can’t see the video (the link is broken).
Also I messed things up. This line should be edit: this change is a complete nonsense lol, just ignore Mathf.Abs (code below this line is reverted)
if(Mathf.Abs(1f + dot) <= 1E-4f) return Quaternion.Euler(180f, 0f, 0f);
All in all, this was supposed to be a function, for example
Quaternion globalRotation(Vector3 point, Vector3 sphereCenter, Vector3 up) {
var localUp = (point - sphereCenter).normalized;
var dot = Vector3.Dot(up, localUp);
if(1f + dot <= 1E-4f) return Quaternion.Euler(180f, 0f, 0f);
return Quaternion.FromToRotation(up, localUp);
}
You can then simply apply this to anything on your planet.
myCube.localRotation = globalRotation(myCube.position, Vector3.zero, Vector3.up);
Or, if you want to apply it to other vectors, for example, a forward looking eyesight
myEyesightDirection = globalRotation(myBuddy.position, Vector3.zero, Vector3.up) * Vector3.forward;
edit:
Also it’s important to note that Quaternion.Euler(180f, 0f, 0f) only works if your up vector wasn’t collinear with X axis to begin with, so this method can probably be made a bit smarter. I typically use my own version of FromToRotation to which I supply the antiparallel rotation in the call site.
Something like this would suffice (I think)
Quaternion globalRotation(Vector3 point, Vector3 sphereCenter, Vector3 up) {
var localUp = (point - sphereCenter).normalized;
var dot = Vector3.Dot(up, localUp);
if(1f + dot <= 1E-4f) {
var q = Quaternion.Euler(180f, 0f, 0f);
if((q * up - up).sqrMagnitude < 1E-7f) q = Quaternion.Euler(0f, 0f, 180f);
return q;
}
return Quaternion.FromToRotation(up, localUp);
}
Mmnah it works, but something’s smelly. I’m sure there’s a more elegant approach, I would have to think some more about it.