Hi, I’ve recently started using Unity, and implemented a Camera that can be dragged with the mouse. However, the camera immediately stops when the mouse button is no longer pressed, but I want it to go on for a while. I’m not too experienced with Unity’s API yet, so I’m not sure on what to do.
CameraScroll.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class CameraScroll : MonoBehaviour
{
[SerializeField]
private Camera mainCamera;
Vector3 touchStart;
[SerializeField]
private Bounds camBounds;
private Vector3 targetPos;
private float speed = 2.0f;
// Start is called before the first frame update
void Start()
{
float height = mainCamera.orthographicSize;
float width = height * mainCamera.aspect;
float minX = Globals.worldBounds.min.x + width;
float maxX = Globals.worldBounds.max.x - width;
camBounds = new Bounds();
camBounds.SetMinMax(
new Vector3(minX, mainCamera.transform.position.y, 0),
new Vector3(maxX, mainCamera.transform.position.y, 0)
);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) {
touchStart = mainCamera.ScreenToWorldPoint(Input.mousePosition);
}
if (Input.GetMouseButton(0)) {
Vector3 direction = touchStart - mainCamera.ScreenToWorldPoint(Input.mousePosition);
targetPos = mainCamera.transform.position + direction;
targetPos = GetCameraBounds();
mainCamera.transform.position = targetPos;
}
}
private Vector3 GetCameraBounds() {
return new Vector3(
Mathf.Clamp(targetPos.x, camBounds.min.x, camBounds.max.x),
transform.position.y,
transform.position.z
);
}
}
For context, I have separate scripts for the bounds, which get the bounds from a SpriteRenderer.