1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-29 03:24:30 +00:00
gravity-fusion/Assets/Components/CameraPan.cs

32 lines
991 B
C#
Raw Normal View History

2019-10-05 09:48:36 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
2019-10-05 18:41:07 +00:00
public class CameraPan : MonoBehaviour
2019-10-05 09:48:36 +00:00
{
2019-10-05 18:41:07 +00:00
public string axisName;
2019-10-05 09:48:36 +00:00
2019-10-05 18:41:07 +00:00
protected new Camera camera;
2019-10-05 09:48:36 +00:00
protected Vector3? lastMousePosition;
private void Start() {
lastMousePosition = null;
camera = GetComponent<Camera>();
}
private void Update() {
2019-10-05 18:41:07 +00:00
bool panIsPressed = Input.GetAxisRaw(axisName) != 0f;
2019-10-05 09:48:36 +00:00
Vector3? currentMousePosition = null;
2019-10-05 18:41:07 +00:00
if(panIsPressed) {
2019-10-05 09:48:36 +00:00
currentMousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
if(lastMousePosition.HasValue) {
Vector3 positionDelta = lastMousePosition.Value - currentMousePosition.Value;
camera.transform.position += positionDelta;
currentMousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
}
}
lastMousePosition = currentMousePosition;
}
}