1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-23 00:34:20 +00:00
gravity-fusion/Assets/Components/CameraPan.cs

95 lines
2.9 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-14 23:36:48 +00:00
{
2019-10-05 18:41:07 +00:00
public string axisName;
2019-10-05 09:48:36 +00:00
2019-10-14 23:36:48 +00:00
protected Vector3? lastWorldPosition;
2019-10-05 09:48:36 +00:00
2019-10-07 15:50:34 +00:00
protected GameController gameController;
2019-10-14 23:36:48 +00:00
protected float panWasPressedFor;
protected float? previousDistance;
protected void Awake()
{
2019-10-07 15:50:34 +00:00
gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
}
2019-10-14 23:36:48 +00:00
private void Start()
{
lastWorldPosition = null;
if (Application.platform == RuntimePlatform.Android)
{
Input.simulateMouseWithTouches = false;
}
panWasPressedFor = 0f;
2019-10-05 09:48:36 +00:00
}
2019-10-14 23:36:48 +00:00
private void Update()
{
bool panIsPressed = false;
switch (Application.platform)
{
case RuntimePlatform.Android:
panIsPressed = Input.touchCount >= 2;
break;
default:
panIsPressed = Input.GetAxisRaw(axisName) != 0f;
break;
}
2019-10-05 18:41:07 +00:00
if(panIsPressed) {
2019-10-14 23:36:48 +00:00
panWasPressedFor += Time.deltaTime;
}
else {
panWasPressedFor = 0f;
}
if (Application.platform != RuntimePlatform.Android && panIsPressed || panWasPressedFor >= 0.2f)
{
float currentDistance;
Vector2 currentScreenPosition;
if (Application.platform == RuntimePlatform.Android)
{
Touch touchA = Input.GetTouch(0);
Touch touchB = Input.GetTouch(1);
Vector2 touchPositionTotal = touchA.position + touchB.position;
currentScreenPosition = new Vector2(touchPositionTotal.x / Input.touchCount, touchPositionTotal.y / Input.touchCount);
}
else
{
currentScreenPosition = Input.mousePosition;
}
Vector3 currentWorldPosition = Camera.main.ScreenToWorldPoint(currentScreenPosition);
if (lastWorldPosition.HasValue)
{
Vector3 positionDelta = lastWorldPosition.Value - currentWorldPosition;
2019-10-07 11:44:26 +00:00
Camera.main.transform.position += positionDelta;
2019-10-14 23:36:48 +00:00
//Don't vibrate the camera!
currentWorldPosition = Camera.main.ScreenToWorldPoint(currentScreenPosition);
2019-10-05 09:48:36 +00:00
}
2019-10-14 23:36:48 +00:00
lastWorldPosition = currentWorldPosition;
}
else {
lastWorldPosition = null;
2019-10-05 09:48:36 +00:00
}
2019-10-14 23:36:48 +00:00
2019-10-06 22:26:32 +00:00
if(Input.GetAxisRaw("ResetCamera") > 0) {
2019-10-07 15:50:34 +00:00
if(gameController.blackHole != null) {
Camera.main.transform.position = new Vector3(gameController.blackHole.transform.position.x,
2019-10-14 23:36:48 +00:00
gameController.blackHole.transform.position.y,
Camera.main.transform.position.z);
2019-10-07 15:50:34 +00:00
}
2019-10-06 22:26:32 +00:00
}
2019-10-05 09:48:36 +00:00
}
}