mirror of
				https://github.com/DerTyp7/industrialize-unity.git
				synced 2025-10-30 21:07:11 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			100 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using UnityEngine;
 | |
| 
 | |
| public class CameraMovement : MonoBehaviour
 | |
| {
 | |
|     [Header("Speed")]
 | |
|     [SerializeField]
 | |
|     float speed = 100f;
 | |
| 
 | |
|     [SerializeField]
 | |
|     float fastSpeed = 200f;
 | |
| 
 | |
|     [SerializeField]
 | |
|     float slowSpeed = 30f;
 | |
| 
 | |
|     [Header("Zoom")]
 | |
|     [SerializeField]
 | |
|     float maxCameraSize = 100f;
 | |
| 
 | |
|     [SerializeField]
 | |
|     float minCameraSize = 3f;
 | |
| 
 | |
|     [SerializeField]
 | |
|     float cameraSizeSteps = 3f;
 | |
| 
 | |
|     Vector2 cameraMovement;
 | |
|     float currentSpeed = 0.0f;
 | |
| 
 | |
|     bool drag = false;
 | |
| 
 | |
|     Vector3 origin;
 | |
|     Vector3 difference;
 | |
| 
 | |
|     Camera cam;
 | |
|     Rigidbody2D rb;
 | |
| 
 | |
|     void Start()
 | |
|     {
 | |
|         cam = GetComponent<Camera>();
 | |
|         rb = GetComponent<Rigidbody2D>();
 | |
|     }
 | |
| 
 | |
|     void LateUpdate()
 | |
|     {
 | |
|         if (Input.GetMouseButton(2))
 | |
|         {
 | |
|             difference = (cam.ScreenToWorldPoint(Input.mousePosition) - cam.transform.position);
 | |
|             if (!drag)
 | |
|             {
 | |
|                 drag = true;
 | |
|                 origin = cam.ScreenToWorldPoint(Input.mousePosition);
 | |
|             }
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             drag = false;
 | |
|         }
 | |
| 
 | |
|         if (drag)
 | |
|         {
 | |
|             cam.transform.position = origin - difference;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     void Update()
 | |
|     {
 | |
|         currentSpeed = speed;
 | |
| 
 | |
|         cameraMovement.x = Input.GetAxis("Horizontal");
 | |
|         cameraMovement.y = Input.GetAxis("Vertical");
 | |
| 
 | |
|         if (Input.GetButton("CameraFast"))
 | |
|             currentSpeed = fastSpeed;
 | |
|         else if (Input.GetButton("CameraSlow"))
 | |
|             currentSpeed = slowSpeed;
 | |
| 
 | |
| 
 | |
|         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
 | |
|             ZoomCameraToPoint(cam.ScreenToWorldPoint(Input.mousePosition), cameraSizeSteps);
 | |
|         else if (Input.GetAxis("Mouse ScrollWheel") < 0f)
 | |
|             ZoomCameraToPoint(cam.ScreenToWorldPoint(Input.mousePosition), -cameraSizeSteps);
 | |
| 
 | |
| 
 | |
|     }
 | |
| 
 | |
|     void FixedUpdate()
 | |
|     {
 | |
|         rb.MovePosition(rb.position + cameraMovement * currentSpeed * Time.fixedDeltaTime);
 | |
|     }
 | |
| 
 | |
|     void ZoomCameraToPoint(Vector3 point, float amount)
 | |
|     {
 | |
|         float multiplier = (1.0f / cam.orthographicSize * amount);
 | |
| 
 | |
|         transform.position += (point - transform.position) * multiplier;
 | |
| 
 | |
|         cam.orthographicSize -= amount;
 | |
| 
 | |
|         cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, minCameraSize, maxCameraSize);
 | |
|     }
 | |
| } | 
