Advertisement
RBECoder

Unity3d Farmville Camera Script Touch/Mouse V2.7 advanced

Mar 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.33 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. /*  3D Camera Movement script V2.7 e.g. like Clash of Clans with full Touch support
  4.  * C 2015 Marcel Ochsendorf (marcel.ochsendorf@gmail.com) rbnetworks.de
  5.  *
  6.  * */
  7.  
  8.  
  9.  
  10.  
  11. /* Enables : true,true,true,true,true,false,true,true,true
  12.  * curr_rot_angle = 0;
  13.  * cam_view_rad = 5;
  14.  * circle_height = 5;
  15.  * cam_offset = {0,1,0}
  16.  * zoom_step = 1;
  17.  * zoom_step_min = 0,2;
  18.  * toom_step_ma = 1;
  19.  * final_cm_transform = MAIN CAMERA (TRANSFORM OBJECT)
  20.  * zoom_speed_pc_mouse = 1;
  21.  * zoom_speed_pc_keyboar = 1;
  22.  * screen_boarder_to_move = 10;
  23.  * camera_movement_speed_pc_keyboar = 5;
  24.  * camera_movement_speed_pc_mouse = 3;
  25.  * camera_rot_speed_pc = 10;
  26.  * zoom_speed_multiplier_mobile = 0.2;
  27.  * cam_move_speed_mobile = 3;
  28.  * rotation_speed_multiplier_mobile = 10;
  29.  * dnymaic_slope_start = 0.7;
  30.  * dynamic_solope_end = 2;
  31.  * */
  32. public class camera_movement_v2 : MonoBehaviour {
  33.  
  34.     public Transform final_camera_transform;
  35.     public Transform camera_pos_offset_init_transform;
  36.  
  37.  
  38.  
  39.     //public const string comment = "GENERAL CAMERA SETTINGS";
  40.     public bool enable_all_camera_interations; //set true if the game is in pause menu or sth else
  41.     public bool enable_zoom;
  42.     public bool enable_movement;
  43.     public bool enable_rotation;
  44.     public bool enable_mouse;
  45.     public bool invert_input;
  46.     public bool enable_dynamic_slope;
  47.     public bool enable_touch_input;
  48.     public bool enable_keyboard;
  49.  
  50.  
  51.     private Vector3 circle_rotation_point;
  52.     public float current_roation_angle; //hier ist der winkel der drehung angegen von 0 bis 360°
  53.     public float camera_view_radius; //hier ist der radius der drehung angegeben je grösser desto
  54.     public float circle_height;
  55.     private Vector3 circle_middle_point;
  56.     public Vector3 camera_offset;
  57.     public float zoom_step;
  58.     public float zoom_step_min;
  59.     public float zoom_step_max;
  60.     private Vector3 slope_vector;
  61.     private bool can_zoom_in, can_zoom_out;
  62.  
  63.     private int intert_input_value;
  64.     private Vector2 current_mouse_pos;
  65.     //public const string comment = "PC CAMERA SETTINGS";
  66.     public float zoom_speed_multiplier_pc_mouse;
  67.     public float zoom_speed_multiplier_pc_keyboard;
  68.     public float screen_boarder_to_move;
  69.     public float camera_movement_speed_pc_keyboard;
  70.     public float camera_movement_speed_pc_mouse;
  71.     public float camera_rotation_speed_pc;
  72.     //public const string comment = "MOBILE PLATTFORM SETTINGS";
  73.     public float zoom_speed_multiplier_mobile;
  74.     public float camera_movement_speed_mobile;
  75.     public float rotation_speed_multiplier_mobile;
  76.     //public const string comment = "DYNAMIC CAMERA SLOPE SETTINGS";
  77.     public float dynamic_slope_start_zoom;
  78.     public float dynamic_slope_end_height;
  79.     private float dynamic_slope_interval;
  80.     private float current_height;
  81.     private Vector2 last_mouse_pos;
  82.     private Vector2 delta_mouse_pos;
  83.     //private Vector3 circle_level_vector;
  84.     //private float slope_vector_angle;
  85.  
  86.  
  87.     // Use this for initialization
  88.     void Start () {
  89.         this.name = "camera_controller";
  90.         can_zoom_in = true;
  91.         can_zoom_out = true;
  92.         current_height = circle_height;
  93.         last_mouse_pos = Input.mousePosition;
  94.         //camera_offset = camera_pos_offset_init_transform.transform.position;
  95.     }
  96.    
  97.     // Update is called once per frame
  98.     void FixedUpdate () {
  99.  
  100.         if((Application.isEditor || !Application.isMobilePlatform) && enable_all_camera_interations){
  101.            
  102.             if(enable_keyboard){
  103.             //zoom by keyboard
  104.             if (Input.GetKey(KeyCode.Plus) && can_zoom_in){zoom_step -= 1*zoom_speed_multiplier_pc_keyboard* Time.deltaTime;}
  105.             if (Input.GetKey (KeyCode.Minus)&& can_zoom_out){zoom_step += 1*zoom_speed_multiplier_pc_keyboard* Time.deltaTime;}
  106.             //camera_movement by keyboard
  107.                 if (Input.GetKey (KeyCode.UpArrow)   || Input.GetKey (KeyCode.W)){move_camera(1,camera_movement_speed_pc_keyboard);} //up
  108.                 if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S)){move_camera(2,camera_movement_speed_pc_keyboard);} //down
  109.                 if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)){move_camera(3,camera_movement_speed_pc_keyboard);} //left
  110.                 if (Input.GetKey (KeyCode.RightArrow)|| Input.GetKey (KeyCode.D)){move_camera(4,camera_movement_speed_pc_keyboard);} //right
  111.             //camera_roation by pc
  112.                 if (Input.GetKey (KeyCode.Q) && enable_rotation){current_roation_angle +=camera_rotation_speed_pc * Time.deltaTime;}
  113.                 if (Input.GetKey (KeyCode.E) && enable_rotation){current_roation_angle -=camera_rotation_speed_pc * Time.deltaTime;}
  114.             }
  115.  
  116.             //camera movement by mouse
  117.             if(enable_mouse){
  118.                 current_mouse_pos = Input.mousePosition;//get mouse position
  119.                 delta_mouse_pos = current_mouse_pos -last_mouse_pos;
  120.                 last_mouse_pos = current_mouse_pos;
  121.                 //zoom by mousewheel
  122.                 if(!Input.GetMouseButton(2)){
  123.                     //zoom_step += (Input.mouseScrollDelta.x+Input.mouseScrollDelta.y) * zoom_speed_multiplier_pc_mouse * Time.deltaTime;
  124.                     zoom_step += Input.GetAxis("Mouse ScrollWheel")* zoom_speed_multiplier_pc_mouse * Time.deltaTime;
  125.                 }
  126.                 //rotation by mousewheel
  127.                 if(Input.GetMouseButton(2) && enable_rotation){
  128.                     current_roation_angle += (delta_mouse_pos.x + delta_mouse_pos.y) * camera_rotation_speed_pc * Time.deltaTime;
  129.                 }
  130.                 //movement by mouse
  131.                 if(!Input.GetMouseButton(2)){
  132.                     if(current_mouse_pos.x < screen_boarder_to_move){move_camera(3,camera_movement_speed_pc_mouse);}//left
  133.                     if(current_mouse_pos.x > Screen.width - screen_boarder_to_move){move_camera(4,camera_movement_speed_pc_mouse);} //right
  134.                     if(current_mouse_pos.y < screen_boarder_to_move){move_camera(2,camera_movement_speed_pc_mouse);} //down
  135.                     if(current_mouse_pos.y > Screen.height - screen_boarder_to_move){move_camera(1,camera_movement_speed_pc_mouse);}//up
  136.             }
  137.             }//ende input mouse button
  138.         }
  139.  
  140.    
  141.  
  142.         //W
  143.         //camera_offset += new Vector3(0.05f*intert_input_value*Mathf.Sin(current_roation_angle*Mathf.Deg2Rad),0,-0.05f*intert_input_value*Mathf.Cos(current_roation_angle*Mathf.Deg2Rad));
  144.  
  145.  
  146.         //S
  147.         //camera_offset += new Vector3(-0.05f*intert_input_value*Mathf.Cos(current_roation_angle*Mathf.Deg2Rad),0,-0.05f*intert_input_value*Mathf.Sin(current_roation_angle*Mathf.Deg2Rad));
  148.         if((Application.isMobilePlatform || Application.isEditor) && enable_touch_input && enable_all_camera_interations){
  149.         Touch[] touches = Input.touches;
  150.         if(touches.Length >0){
  151.             //move singel toche
  152.                 if(touches.Length == 1 && enable_movement){
  153.                 if(touches[0].phase == TouchPhase.Moved){
  154.                     Vector2 tmp_delta = touches[0].deltaPosition;
  155.                     float pos_y = tmp_delta.x*-intert_input_value;
  156.                     float pos_x = tmp_delta.y*-intert_input_value;
  157.                    
  158.                     move_camera(1,pos_x*camera_movement_speed_mobile); //for up and down direction the can be pos_x > 0  && pos_x < 0
  159.                     move_camera(3,pos_y*camera_movement_speed_mobile);
  160.                     }
  161.             }// t == 1
  162.             //zoom double touche
  163.             if(touches.Length == 2 && enable_zoom){
  164.                 Touch touch_finger_1 = touches[0];
  165.                 Touch touch_finger_2 = touches[1];
  166.                 Vector2 delta_touch_finger_1 = touch_finger_1.position - touch_finger_1.deltaPosition;
  167.                 Vector2 delta_touch_finger_2 = touch_finger_2.position - touch_finger_2.deltaPosition;
  168.                 float mag_prev_touch = (delta_touch_finger_1 - delta_touch_finger_2).magnitude;
  169.                 float mag_delta_touch = (touch_finger_1.position - touch_finger_2.position).magnitude;
  170.                 float delta_mag = mag_delta_touch - mag_prev_touch;
  171.  
  172.                 //check if we can zoom in or out
  173.                 if(zoom_speed_multiplier_mobile*delta_mag*-intert_input_value*Time.deltaTime > 0 && can_zoom_in){
  174.                     zoom_step+=zoom_speed_multiplier_mobile*delta_mag*intert_input_value*Time.deltaTime;
  175.                 }
  176.                 if(zoom_speed_multiplier_mobile*delta_mag*-intert_input_value*Time.deltaTime < 0 && can_zoom_out){
  177.                     zoom_step+=zoom_speed_multiplier_mobile*delta_mag*intert_input_value*Time.deltaTime;
  178.                 }
  179.            
  180.             }// t == 2
  181.  
  182.  
  183.  
  184.                 //rotation by touch
  185.                 if(touches.Length == 3 && enable_rotation){
  186.                     Touch touch_finger_1 = touches[0];
  187.                     Touch touch_finger_2 = touches[1];
  188.                     Vector2 delta_touch_finger_1 = touch_finger_1.position - touch_finger_1.deltaPosition;
  189.                     Vector2 delta_touch_finger_2 = touch_finger_2.position - touch_finger_2.deltaPosition;
  190.                     float mag_prev_touch = (delta_touch_finger_1 - delta_touch_finger_2).magnitude;
  191.                     float mag_delta_touch = (touch_finger_1.position - touch_finger_2.position).magnitude;
  192.                     float delta_mag = mag_delta_touch - mag_prev_touch;
  193.                    
  194.                     //check if we can zoom in or out
  195.                     if(zoom_speed_multiplier_mobile*delta_mag*-intert_input_value*Time.deltaTime > 0 ){
  196.                         current_roation_angle+=rotation_speed_multiplier_mobile*delta_mag*intert_input_value*Time.deltaTime;
  197.                     }
  198.                     if(zoom_speed_multiplier_mobile*delta_mag*-intert_input_value*Time.deltaTime < 0 ){
  199.                         current_roation_angle+=rotation_speed_multiplier_mobile*delta_mag*intert_input_value*Time.deltaTime;
  200.                     }
  201.                    
  202.                 }// t == 3
  203.  
  204.         }//ende t >0
  205.         }//ende isMobile
  206.  
  207.  
  208.  
  209.         //some clamp stuff
  210.         if(current_roation_angle > 360.0f){current_roation_angle -= 360.0f;}
  211.         if(current_roation_angle < 0.0f){current_roation_angle += 360.0f;}
  212.         if(camera_view_radius < 0.0f){camera_view_radius = 0.0f;}
  213.         if(zoom_step <= zoom_step_min){zoom_step = zoom_step_min;can_zoom_out = true;can_zoom_in = false;}
  214.         if(zoom_step >= zoom_step_max){zoom_step = zoom_step_max;can_zoom_out = false;can_zoom_in = true;}
  215.         if(zoom_step > zoom_step_min && zoom_step < zoom_step_max){can_zoom_out = true;can_zoom_in = true;}
  216.         if(!invert_input){intert_input_value = -1;}else{intert_input_value = 1;}
  217.         //Mathf.Clamp(current_roation_angle,0.0f,360.0f);
  218.  
  219.         //Here is the dynamic slope code
  220.         if(dynamic_slope_start_zoom > zoom_step && enable_dynamic_slope && dynamic_slope_start_zoom > zoom_step_min){ // is the dyn slope aktive and in the rage( see settings)
  221.             dynamic_slope_interval = ((zoom_step-zoom_step_min)*100) / (dynamic_slope_start_zoom-zoom_step_min) ; //so first we calculate the current percentage of the zoom_state so with this value 0-100 we can calulate the current height
  222.             current_height =  (circle_height)/100*dynamic_slope_interval; //now calculate the current cameraheight.. form the percentage zoom process
  223.             if(current_height < dynamic_slope_end_height){current_height = dynamic_slope_end_height;} //if the maximum cameraheight reaced we cant set the heigt further
  224.         }else{
  225.             current_height = circle_height;
  226.         }
  227.  
  228.  
  229.         //calculate the point on the circle track at the specific angle
  230.         circle_rotation_point = new Vector3((Mathf.Cos(Mathf.Deg2Rad*current_roation_angle)*camera_view_radius)+camera_offset.x,current_height+circle_middle_point.y,(Mathf.Sin(Mathf.Deg2Rad*current_roation_angle)*camera_view_radius)+camera_offset.z);
  231.         //calculate the center of the circle with the 3 point method // you can also use 2 vectors in a 90° angle and calculate the intersection
  232.         Vector2 circle_point_a = new Vector3(Mathf.Cos(Mathf.Deg2Rad*0.0f)*camera_view_radius,Mathf.Sin(Mathf.Deg2Rad*0.0f)*camera_view_radius);
  233.         Vector2 circle_point_b = new Vector3(Mathf.Cos(Mathf.Deg2Rad*45.0f)*camera_view_radius,Mathf.Sin(Mathf.Deg2Rad*45.0f)*camera_view_radius);
  234.         Vector2 circle_point_c = new Vector3(Mathf.Cos(Mathf.Deg2Rad*90.0f)*camera_view_radius,Mathf.Sin(Mathf.Deg2Rad*90.0f)*camera_view_radius);
  235.         //so now we habe the 3 points on the circle now we can calculate the x/y  coords
  236.         float A11=circle_point_a.x-circle_point_b.x;
  237.         float A12=circle_point_a.y-circle_point_b.y;
  238.         float A21=circle_point_a.x-circle_point_c.x;
  239.         float A22=circle_point_a.y-circle_point_c.y;
  240.         float B1=A11*(circle_point_a.x+circle_point_b.x)+A12*(circle_point_a.y+circle_point_b.y);
  241.         float B2=A21*(circle_point_a.x+circle_point_c.x)+A22*(circle_point_a.y+circle_point_c.y);
  242.         float D=2*(A11*A22-A21*A12);
  243.         float Dx=B1*A22-B2*A12;
  244.         float Dy=A11*B2-A21*B1;
  245.         //float x=Dx/D, y=Dy/D; //here are the final x/y coordinates in a further step we add the height information (Y Axis) to create the final position Vector
  246.         circle_middle_point = new Vector3(Dx/D,0.0f,Dy/D)+camera_offset;
  247.         //now we calculate the slope form the center of the circle to the point on the circle(at the specific angle)
  248.         slope_vector = circle_rotation_point-circle_middle_point;
  249.         //so now we add a zoom wuhuuuu we move the object(camera) along the slope vector
  250.         final_camera_transform.position = new Vector3(slope_vector.x*zoom_step,slope_vector.y*zoom_step,slope_vector.z*zoom_step)+camera_offset;
  251.         //now we have the slopevector so we can calculate the angle of the slope relativ to the circle we need this to rotate the object(camera) into the right direction
  252.         //slope_vector
  253.         //circle_level_vector
  254.         //float dot_product = Vector3.Dot(circle_middle_point,slope_vector); //this is the dot Product funktion its easier to use this
  255.         //dot_product = dot_product/ (circle_middle_point.magnitude * slope_vector.magnitude);
  256.         //slope_vector_angle = 180-(Mathf.Acos(dot_product)*Mathf.Rad2Deg);
  257.         //final_camera_transform.rotation = Quaternion.AngleAxis(slope_vector_angle, Vector3.right);
  258.             final_camera_transform.LookAt(circle_middle_point);//its a simplet funktion to rotate an objet to an spcific vector... this is our circle_middlepoint
  259.         //some debug stuff
  260.         //Debug.DrawLine (circle_middle_point, circle_rotation_point, Color.cyan);
  261.  
  262.         //this.transform.position = final_camera_transform.position;
  263.         //this.transform.rotation = final_camera_transform.rotation;
  264.  
  265.     }
  266.  
  267.  
  268.  
  269.  
  270.  
  271.     public void move_camera(int dir, float _move_speed){
  272.  
  273.         switch (dir) {
  274.         case 1: //W
  275.             camera_offset += new Vector3(0.05f*intert_input_value*Mathf.Cos(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime,0,0.05f*intert_input_value*Mathf.Sin(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime);
  276.             break;
  277.         case 2: //S
  278.             camera_offset += new Vector3(-0.05f*intert_input_value*Mathf.Cos(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime,0,-0.05f*intert_input_value*Mathf.Sin(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime);
  279.             break;
  280.         case 3: //A
  281.             camera_offset += new Vector3(-0.05f* intert_input_value*Mathf.Sin(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime,0,0.05f*intert_input_value*Mathf.Cos(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime);
  282.             break;
  283.         case 4: //D
  284.             camera_offset += new Vector3(0.05f*intert_input_value*Mathf.Sin(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime,0,-0.05f*intert_input_value*Mathf.Cos(current_roation_angle*Mathf.Deg2Rad)*_move_speed*Time.deltaTime);
  285.             break;
  286.         default:
  287.             break;
  288.         }//ende scwitch
  289.     }//ende move camera
  290.  
  291.  
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement