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

35 lines
1.1 KiB
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;
2019-10-06 22:26:32 +00:00
if(Input.GetAxisRaw("ResetCamera") > 0) {
camera.transform.position = new Vector3(0, 0, camera.transform.position.z);
}
2019-10-05 09:48:36 +00:00
}
}