Search Unity

Control panel + mouse + keyboard camera control - problem :(

Discussion in 'Scripting' started by rasq, Jun 15, 2009.

  1. rasq

    rasq

    Joined:
    May 25, 2009
    Posts:
    51
    Hi, today i have new problem, i must merge 3 diffrent camera control styles into one.

    1) mouse look script:

    Code (csharp):
    1. var target : Transform;
    2.  
    3. var xspeed = 250.0;
    4. var yspeed = 120.0;
    5.  
    6. var yMinLimit = -20;
    7. var yMaxLimit = 80;
    8.  
    9. private var xa = 0.0;
    10. private var ya = 0.0;
    11.  
    12.  
    13. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    14.  
    15.  
    16.  
    17. function Start () {
    18.     var angles = transform.eulerAngles;
    19.     xa = angles.y;
    20.     ya = angles.x;
    21. }
    22.  
    23. function Update() {
    24.  
    25.     if (!target) {return;}
    26.    
    27.     if (Input.GetKey (KeyCode.RightControl)) {
    28.             rozgladanie ();
    29.     }
    30.  
    31.  
    32.  
    33. function rozgladanie (){
    34.  
    35.         var rot = Quaternion.Euler(ya, xa, 0);
    36.        
    37.         xa += Input.GetAxis("Mouse X") * xspeed * 0.02;
    38.        
    39.         ya -= Input.GetAxis("Mouse Y") * yspeed * 0.02;
    40.        
    41.         ya = ClampAngle(ya, yMinLimit, yMaxLimit);
    42.        
    43.         MainCamera.transform.rotation = rot;
    44. }
    45.  
    46.  
    47.  
    48. static function ClampAngle (angle : float, min : float, max : float) {
    49.    if (angle < -360)
    50.       angle += 360;
    51.    if (angle > 360)
    52.       angle -= 360;
    53.    return Mathf.Clamp (angle, min, max);
    54. }

    2. control panel orbit and camera move:

    Code (csharp):
    1. x = Input.GetAxis("Horizontal") * Time.deltaTime * speedmove;
    2.     y = Input.GetAxis("Vertical") * Time.deltaTime * speedmove;
    3.     z = Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * speedmove;
    4.    
    5.        
    6.    
    7.    
    8.    
    9.         if(Input.GetKeyDown(KeyCode.KeypadPlus)) {
    10.        
    11.             z = z + przesuniecie * speedmove;
    12.            
    13.             while(Input.GetKeyUp(KeyCode.KeypadPlus)){}
    14.         }
    15.        
    16.         if(Input.GetKeyDown(KeyCode.KeypadMinus))  {
    17.             z = z - przesuniecie * speedmove;
    18.         }
    19.            
    20.     transform.Translate(x, y, z);
    21.    
    22.  
    23.  
    24. if (gora.ruch == true){
    25.         y = (y + przesuniecie) * Time.deltaTime * speed;
    26.         transform.Translate(0, y, 0);
    27.         }  
    28.        
    29.         if (dol.ruch == true){
    30.                     y = (y - przesuniecie) * Time.deltaTime * speed;
    31.                     transform.Translate(0, y, 0);
    32.                     }
    33.                    
    34.                     if (lewo.ruch == true){
    35.                                     x = (x - przesuniecie) * Time.deltaTime * speed;
    36.                                     transform.Translate(x, 0, 0);
    37.                                     }
    38.                                    
    39.                                     if (prawo.ruch == true){
    40.                                                 x = (x + przesuniecie) * Time.deltaTime * speed;
    41.                                                 transform.Translate(x, 0, 0);
    42.                                                 }
    43.                                                
    44.                                                 if (zoomi.ruch == true){
    45.                                                             z = (z + przesuniecie) * Time.deltaTime * speed;
    46.                                                             transform.Translate(0, 0, z);
    47.                                                             }
    48.                                                            
    49.                                                             if (zoomo.ruch == true){
    50.                                                                             z = (z - przesuniecie) * Time.deltaTime * speed;
    51.                                                                             transform.Translate(0, 0, z);
    52.                                                                             }
    53.    
    54.    
    55.  
    56.  
    57.  
    58.        
    59.    
    60.     if (wprzod.ruch == true){
    61.     transform.Rotate(-Vector3.right * Time.deltaTime * speed);
    62.         }  
    63.        
    64.         if (wtyl.ruch == true){
    65.         transform.Rotate(Vector3.right * Time.deltaTime * speed);
    66.                     }
    67.                    
    68.                     if (wlewo.ruch == true){
    69.                     transform.Rotate(Vector3.up * Time.deltaTime * speed, Space.World);
    70.                                     }  
    71.                                    
    72.                                     if (wprawo.ruch == true){
    73.                                     transform.Rotate(-Vector3.up * Time.deltaTime * speed, Space.World);
    74.                                                 }
    75.    
    76.                                                 if (minus.ruch == true){
    77.                                                         transform.Rotate(Vector3.forward * Time.deltaTime * speed);
    78.                                                         }
    79.    
    80.                                                         if (plus.ruch == true){
    81.                                                                 transform.Rotate(-Vector3.forward * Time.deltaTime * speed);
    82.                                                                 }
    83. }


    everything are ok, but, when change angle of camera by control panel, and secound i use mouse look then camera posiotion are reseted.

    i dont know what i can do to send initial posiotion of camera to mouse look script.


    maybe someone can halp me, or tell me some hint???


    thank every one who can try help me. :]