I’ve been working on this problem for a very long time and I’ve almost given up. Seriously, I don’t even want to talk about how long this is taking me.
I’m attempting a 3d camera orbit function. The problem is the camera is in negative local Z space so when I do: Z/Distance for my Z calculation, the sign is always negative! It does not create that nice orbiting feature! I need the camera too smoothly transition from negative Z space to positive and back again if the player so desires.
Some of the code I’m about to supply you is not used, it is a holdover from when I was debugging/testing things out. I just thought I would post the naked code, with few if any changes from the original source.
My code:
bool gameStart = true;
bool inPositiveZSpace = false;
public Vector3 findAngleFromDir (Vector3 posA, Vector3 posB)
{
Vector3 dir = posB;
float horizontal;
float vertical;
float forward;
float dist = Vector3.Distance (posB, Vector3.zero); //Find hypotenuse/distance
print ("Dir computation = " + (dir / dist));
//These are ARCCOS calculations
horizontal = (dir.x / dist); //ArcFoo means the inverse function //RETURNS ONE. POSSIBLY WHY Mathf.COS RETURNS NAN? INVALID INPUT RANGE?
vertical = (dir.y / dist); //Multiplying by Rad2Deg converts the return value from Mathf.ACos to degrees!
forward = -(dir.z / dist);
//+ " Vertical = " + vertical
print ("Dir = " + dir + " Distance = " + dist + " Horizon = " + horizontal + " Vertical = " + vertical + " Forward = " + forward);
return new Vector3 (horizontal, vertical, forward);
}
public Vector3 findCirclePoint (Vector3 angles, Vector2 input, float r)
{
float dist = Vector2.Distance (input, Vector2.zero);
print ("DISTANCE INPUT = " + dist);
float horizon, vertic, acosV, acosH, zSign;
horizon = input.x / dist;
vertic = input.y / dist;
acosV = Mathf.Acos(angles.y) * Mathf.Rad2Deg;
acosH = Mathf.Acos (angles.x) * Mathf.Rad2Deg;
zSign = Mathf.Sign(angles.z);
if ((acosH >= 180 || acosV >= 180) && !inPositiveZSpace) { //If aligned with an axis reverse the sign of the Z component
angles.z = +angles.z;
inPositiveZSpace = true;
print ("In POSITIVE z space!");
} else if ((acosH < 180 || acosV < 180) && inPositiveZSpace){
angles.z = -angles.z;
inPositiveZSpace = false;
print ("In negative z space!");
}
print ("Angles.x = " + acosH + " Angles.y = " + acosV);
return (new Vector3 ((angles.x + horizon), (angles.y + vertic), angles.z) * r);
}
public void panCamera ()
//To solve this I need to find out whats wrong with the Z component computation. Its probably the sign. Or I need to graph a sphere somehow...
{ //Third person orbit function.
print ("==============[START OF FUNCTION CALL]==============");
Vector3 lookAtPos = player.transform.localPosition;
Vector3 cameraPos = myCamera.transform.localPosition;
print ("LookatPos = " + lookAtPos + " cameraPos = " + cameraPos);
if (gameStart) {
float dist = Vector3.Distance (cameraPos, lookAtPos); //Find the distance between camera and view target
Vector3 initialPos = cameraPos * ((camDistance / 2) / dist); //Scale the distance to the limit
Camera.main.transform.localPosition = initialPos;
gameStart = false;
print ("InitialPos = " + initialPos);
} else {
Vector3 angles = findAngleFromDir (lookAtPos, cameraPos);
Vector2 localMInput = relativeMInput / (screenSize * maxMouseDist) * camDistance;
// angles = new Vector3(angles.x + localMInput.x, angles.y + localMInput.y, angles.z);
Vector3 newPos = findCirclePoint (angles, localMInput, camDistance / 2);
Camera.main.transform.localPosition = newPos;
Camera.main.transform.LookAt (player.transform.position);
print ("local mouse input = " + localMInput + " angles = " + angles );
print ("Newpos = " + newPos);
}
}
What am I doing wrong?