Advertisement
shinevision

Untitled

Jul 11th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1.     using UnityEngine;
  2.      
  3.     public class RtsCamera : MonoBehaviour
  4.     {
  5.      
  6.         public float speed = 2.0f;                  // movement speed when scrolling on the side of the screen
  7.         public float zoom_speed = 2.0f;             // zoom speed
  8.         public float speed_x = 200.0f;              // Rotation speed
  9.         float rotation_y = 0.0f;                    // variable used for rotation function
  10.         private int edge_threshold = 5;             // area before the end of the screen where scrolling activate
  11.         // limits
  12.         public float scroll_limit_x = 5f;                // how much you can scroll from the center of the scene on the X axis.
  13.         public float scroll_limit_z = 5f;                // how much you can scroll from the center of the screen on the Y axis.
  14.      
  15.      
  16.         void Start()
  17.         {
  18.             // adapt the limits based on the starting position of the camera.
  19.             // in this way, there will always be an equal amount to the limit value
  20.             // independently from where the starting position is.
  21.             if (transform.position.x > 0)
  22.                 scroll_limit_x += transform.position.x;
  23.             else
  24.                 scroll_limit_x -= transform.position.x;
  25.      
  26.             if (transform.position.z > 0)
  27.                 scroll_limit_z += transform.position.z;
  28.             else
  29.                 scroll_limit_z -= transform.position.z;
  30.         }
  31.      
  32.         void Update()
  33.         {
  34.             float scrollwheel = Input.GetAxis("Mouse ScrollWheel");
  35.             float mouse_x = Input.mousePosition.x;
  36.             float mouse_y = Input.mousePosition.y;
  37.      
  38.             //zoom with scroll wheel; forward to zoom in, backward to scroll out.
  39.             transform.Translate(0, -scrollwheel * zoom_speed, scrollwheel * zoom_speed, Space.World);
  40.      
  41.             // Orbit function using right mouse button pressed.
  42.             if (Input.GetMouseButton(1))
  43.             {
  44.                 rotation_y += Input.GetAxis("Mouse X") * speed_x * Time.deltaTime;
  45.                 transform.localEulerAngles = new Vector3(0, rotation_y, 0);
  46.             }
  47.      
  48.             // movement scrolling on the side of the screen; the threshold define how far to the border
  49.             // is the scrolling activating.
  50.             if (mouse_x >= Screen.width - edge_threshold && transform.position.x <= scroll_limit_x)
  51.             {
  52.                 transform.Translate((Vector3.right * speed * Time.deltaTime), Space.Self);
  53.             }
  54.             else if (mouse_x < edge_threshold && transform.position.x >= -scroll_limit_x)
  55.             {
  56.                 transform.Translate((Vector3.left * speed * Time.deltaTime), Space.Self);
  57.             }
  58.             else if (mouse_y >= Screen.height - edge_threshold && transform.position.z <= scroll_limit_z)
  59.             {
  60.                 transform.Translate((Vector3.forward * speed * Time.deltaTime), Space.Self);
  61.             }
  62.             else if (mouse_y < edge_threshold && transform.position.z >= -scroll_limit_z)
  63.             {
  64.                 transform.Translate((Vector3.back * speed * Time.deltaTime), Space.Self);
  65.             }
  66.      
  67.         }
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement