Problem with camera code need help.

I’m making a fighting game on a side scrolling view and it’s not orthograpich it’s 3d the camera.

my problem is that when both the player get close the camera zooms in too much close, and when both of the player and enemy are far apart from each other the camera moves way to far out of the level.

my question would be how to control this behavior?

here’s the code, all you need is two objects one player and one enemy i got this code from the forums and i fixed it a bit to work but not good enough as of yet.

Code:

//targets to be added, a for enemy and b for player depending where are they located.
var a : Transform;
var b : Transform;

// this code is irrevelant for testing the gui
function OnGUI()
{
GUI.Box ( Rect(10, 10, 100,90), “debugging”);
}

function Update ()
{
//calculations for the camera…
var abHeading : Vector3 = b.position - a.position;
var perp : Vector3 = Vector3.Cross(abHeading, Vector3.up);
var halfway : Vector3 = (a.position + b.position) /2;
var dist : float = Vector3.Distance(a.position, b.position);
var distScale: float = 2.0;
//////////////

halfway.y = 2.0;

///moving the camera the if statement makes the camera invert the code so it stays on the same asxis
camera.transform.position = halfway + perp.normalized * dist * distScale;
if(abHeading.x > 0)
{
camera.transform.position = halfway - perp.normalized * dist * distScale;
}
///////////////////////

//print(dist);
//print(abHeading);
//print(perp);
//print(halfway);
//print(distScale);

}

Because its a side view, you can just conpare the camera transform to its location with transform.position . Like so
just an example off the top of my head and could be completly wrong :smile:
But may give you a general idea of how to compare the position of an object and stop it from going past the two limits :idea:

var playerCam : Camera;
function Update() {
   if(playerCam.transform.position.z <=11) {
   playerCam.transform.position.z = 10.5;
   }
   if(playerCam.transform.position.z >=30) {
   playerCam.transform.position.z = 29.5;
   }
}

thank you so much it works now, i think is that i needed to make the cam a variable somehow it didn’t work before if i just mad3e an if statement in order to put a boundary on z position so it was probably the code overriding it.

than you soo much for your help.