Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Micro Machines Style Camera

Discussion in 'Scripting' started by willgoldstone, Mar 27, 2007.

  1. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    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 -

    Code (csharp):
    1.  
    2. function Update () {
    3.    
    4.     p1pos = GameObject.FindWithTag("Player").transform.position;
    5.     p2pos = GameObject.FindWithTag("Player2").transform.position;
    6.    
    7.     var midPoint = p1pos+p2pos;
    8.     transform.position=midPoint;
    9.    
    10. }
    11.  
    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
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I think your approach sounds reasonable overall, but your code to find the midpoint looks totally wrong to me. Try this instead:

    Code (csharp):
    1. var midPoint = p1pos + ((p2pos - p1pos) * 0.5);
     
  3. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Or even simpler:
    Code (csharp):
    1. var midPoint = (p1pos + p2pos) * 0.5;
    :)
     
  4. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    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?...
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    What you need to do is:
    1) find the distance between them
    2) figure out a factor to multiply this distance by
    3) 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.

    Code (csharp):
    1.  
    2. var fudgeFactor : float = 2.5;
    3. . . .
    4. var height : float = Vector3.Distance(p1pos, p2pos);
    5. height *= fudgeFactor;
    6. midPoint.y+=height;
    7. transform.position=midPoint;
     
  6. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi guys,

    thanks for your help, I've currently got this -

    Code (csharp):
    1.  
    2. function Update () {
    3.    
    4.     if(transform.position.y>=105){
    5.    
    6.         if((HealthP1.health>=11)  (HealthP2.health2>=11)){
    7.    
    8.             p1pos = GameObject.FindWithTag("Player").transform.position;
    9.             p2pos = GameObject.FindWithTag("Player2").transform.position;
    10.  
    11.    
    12.     var midPoint = (p1pos + p2pos) * 0.5;
    13.     var fudgeFactor : float = 1.0;
    14.    
    15.     var height : float = Vector3.Distance(p1pos,p2pos);
    16.     height *= fudgeFactor;
    17.     midPoint.y+=height;
    18.     transform.position=midPoint;
    19.  
    20.         }
    21.     }
    22.    
    23.     else if(transform.position.y<105){
    24.         transform.position.y=110;
    25.     }
    26. }
    27.  
    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 (tm).... see here -

    www.willgoldstone.com/impact/test3.html


    Im not approaching the restriction very well am I!? thoughts?


    Cheers,

    Will
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    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):
    Code (csharp):
    1. function Update () {
    2.    
    3.    
    4.    if((HealthP1.health>=11)  (HealthP2.health2>=11)){
    5.    
    6.       p1pos = GameObject.FindWithTag("Player").transform.position;
    7.       p2pos = GameObject.FindWithTag("Player2").transform.position;
    8.  
    9.    
    10.       var midPoint = (p1pos + p2pos) * 0.5;
    11.       var fudgeFactor = 1.0;
    12.    
    13.       var height = Vector3.Distance(p1pos,p2pos) * fudgeFactor;
    14.       midPoint.y+=height;
    15.       if(midPoint.y < 110.0)
    16.           midPoint.y=110.0;
    17.       transform.position=midPoint;
    18.  
    19.    }
    20. }
    21.  
     
  8. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi everyone,

    thanks to you guys and Mister Tuttle and Neil Carter in the IRC channel, I now have this, which works quite well -

    Code (csharp):
    1.  
    2. var maxZoom = 105;
    3.  
    4.  
    5. function Update () {
    6.    
    7.     var maxZoomPlusOne = maxZoom+1;
    8.    
    9.     if(transform.position.y>maxZoom){
    10.    
    11.         if((HealthP1.health>=11)  (HealthP2.health2>=11)){
    12.    
    13.             p1pos = GameObject.FindWithTag("Player").transform.position;
    14.             p2pos = GameObject.FindWithTag("Player2").transform.position;
    15.  
    16.    
    17.     var midPoint = (p1pos + p2pos) * 0.5;
    18.     var fudgeFactor : float = 1.0;
    19.    
    20.     var height : float = Vector3.Distance(p1pos,p2pos);
    21.     height *= fudgeFactor;
    22.    
    23.             if(height < maxZoom)height=maxZoomPlusOne;
    24.    
    25.     midPoint.y=height;
    26.     transform.position=midPoint;
    27.  
    28.         }
    29.     }  
    30. }
    31.  
    Thanks to everyone for your help, will update -

    www.willgoldstone.com/impact

    with latest version soon!

    - Will