Hi all,
im making a 2 player tank shooter, see
www.willgoldstone.com/impact
And because I want a top down view, im trying to set up a script with a camera that pans and zooms in and out when the vehicles are far apart or closer together, like in micro machines series. so far ive done this -
function Update () {
p1pos = GameObject.FindWithTag("Player").transform.position;
p2pos = GameObject.FindWithTag("Player2").transform.position;
var midPoint = p1pos+p2pos;
transform.position=midPoint;
}
Which is put on an empty GO for the camera to follow using the Smooth Follow component. It vaguely works but not correctly and doesnt really sit in the middle. Am I taking the right approach here?
Any Help much appreciated,
Thanks,
Will
I think your approach sounds reasonable overall, but your code to find the midpoint looks totally wrong to me. Try this instead:
var midPoint = p1pos + ((p2pos - p1pos) * 0.5);
Or even simpler:
var midPoint = (p1pos + p2pos) * 0.5;

Thats great guys, thanks for the help, doesnt quite position it, but i think thats more issue with the smooth follow angle.
now looks like -
www.willgoldstone.com/impact/test2.html
Also, I want to make it zoom out as they drive further apart, but my mid point is a vector3, how should I re-use the midpoint to affect the Y axis of the empty game object this script is on? I need a single figure to define the Y axis… dont i?..
What you need to do is:
- find the distance between them
- figure out a factor to multiply this distance by
- add the value to y
step 2 will be mainly trial and error, based largely on the angle of your camera and how much extra space around the edge you want, etc.
var fudgeFactor : float = 2.5;
. . .
var height : float = Vector3.Distance(p1pos, p2pos);
height *= fudgeFactor;
midPoint.y+=height;
transform.position=midPoint;
Hi guys,
thanks for your help, I’ve currently got this -
function Update () {
if(transform.position.y>=105){
if((HealthP1.health>=11) (HealthP2.health2>=11)){
p1pos = GameObject.FindWithTag("Player").transform.position;
p2pos = GameObject.FindWithTag("Player2").transform.position;
var midPoint = (p1pos + p2pos) * 0.5;
var fudgeFactor : float = 1.0;
var height : float = Vector3.Distance(p1pos,p2pos);
height *= fudgeFactor;
midPoint.y+=height;
transform.position=midPoint;
}
}
else if(transform.position.y<105){
transform.position.y=110;
}
}
Which ive used that if and else if at the top and bottom - what im trying to do there is to stop it zooming in a certain amount, however, it produces Crazy-Vision ™… see here -
www.willgoldstone.com/impact/test3.html
Im not approaching the restriction very well am I!? thoughts?
Cheers,
Will
I guess it happens because you bump the height to 110 only when the height is less than 105 originally.
So next frame, the height is above 105, and you calculate a new height using the distance between the players, which can result in a height lower than 105, and then next frame height is lower than 105, which will bump it again up to 110, and so on toggling between some calculated height and 110 every other frame.
You probably ment to do something like the following (I also removed some redundant “: float” declarations):
function Update () {
if((HealthP1.health>=11) (HealthP2.health2>=11)){
p1pos = GameObject.FindWithTag("Player").transform.position;
p2pos = GameObject.FindWithTag("Player2").transform.position;
var midPoint = (p1pos + p2pos) * 0.5;
var fudgeFactor = 1.0;
var height = Vector3.Distance(p1pos,p2pos) * fudgeFactor;
midPoint.y+=height;
if(midPoint.y < 110.0)
midPoint.y=110.0;
transform.position=midPoint;
}
}
Hi everyone,
thanks to you guys and Mister Tuttle and Neil Carter in the IRC channel, I now have this, which works quite well -
var maxZoom = 105;
function Update () {
var maxZoomPlusOne = maxZoom+1;
if(transform.position.y>maxZoom){
if((HealthP1.health>=11) (HealthP2.health2>=11)){
p1pos = GameObject.FindWithTag("Player").transform.position;
p2pos = GameObject.FindWithTag("Player2").transform.position;
var midPoint = (p1pos + p2pos) * 0.5;
var fudgeFactor : float = 1.0;
var height : float = Vector3.Distance(p1pos,p2pos);
height *= fudgeFactor;
if(height < maxZoom)height=maxZoomPlusOne;
midPoint.y=height;
transform.position=midPoint;
}
}
}
Thanks to everyone for your help, will update -
www.willgoldstone.com/impact
with latest version soon!