Skip to content

Instantly share code, notes, and snippets.

@nicetrysean
Last active October 3, 2022 14:58
Show Gist options
  • Save nicetrysean/56820bdbc3b616c260f0b8f19e06faef to your computer and use it in GitHub Desktop.
Save nicetrysean/56820bdbc3b616c260f0b8f19e06faef to your computer and use it in GitHub Desktop.
Camera Panning via DoTween on click and drag of mouse
using DG.Tweening;
using DG.Tweening.Core;
using DG.Tweening.Plugins.Options;
using UnityEngine;
public class CameraPan : MonoBehaviour
{
[SerializeField] private new Camera camera;
[SerializeField] private Ease easeType = Ease.OutExpo;
[SerializeField] private float inertiaDuration = .33f;
[SerializeField] private float inertiaAmount = 10f;
[SerializeField] private int mouseButton = 0;
[SerializeField] private float mouseSensitivity = 1f;
[SerializeField] private Vector2 xPositionClamp = new Vector2(-100, 100);
[SerializeField] private Vector2 yPositionClamp = new Vector2(-100, 100);
[SerializeField] bool mouseXDirectionInverted;
[SerializeField] bool mouseYDirectionInverted;
private Vector3 _velocity;
private Vector3 _targetPosition;
private TweenerCore<Vector3, Vector3, VectorOptions> _tween;
private void OnValidate()
{
// If we don't have a camera, skip this!
if (Camera.main == null)
{
Debug.LogError("No main camera found");
return;
}
// Find camera automatically and cache it
camera = Camera.main;
}
private void Start()
{
// Set our target as what our camera is currently set to
_targetPosition = camera.transform.localPosition;
}
private void Update()
{
// if we don't have a camera, don't do anything
if (!camera) return;
MoveCameraByMousePositionDelta();
}
private void MoveCameraByMousePositionDelta()
{
var movingObjectTransform = camera.transform;
float mouseInputX = 0;
float mouseInputY = 0;
if (Input.GetMouseButtonDown(mouseButton)) // Mouse Button Pressed
{
// Reset on mouse down
// This is to prevent the camera from moving based on the previous mouse velocity
_velocity = Vector3.zero;
_targetPosition = movingObjectTransform.localPosition;
}
if (Input.GetMouseButton(mouseButton)) // Mouse Button is held down
{
// Get mouse input
mouseInputY = Input.GetAxis("Mouse Y") * mouseSensitivity * (mouseXDirectionInverted ? 1 : -1);
mouseInputX = Input.GetAxis("Mouse X") * mouseSensitivity * (mouseYDirectionInverted ? 1 : -1);
// Clean up old tween if it exists
if (_tween != null && _tween.IsActive())
{
_tween.Kill();
}
// If we have movement, then we update our target position
if (mouseInputX != 0 || mouseInputY != 0)
{
// Clamp target position
var targetX = Mathf.Clamp(_targetPosition.x + mouseInputX, xPositionClamp.x, xPositionClamp.y);
var targetY = Mathf.Clamp(_targetPosition.y + mouseInputY, yPositionClamp.x, yPositionClamp.y);
// Set Target
_targetPosition = new Vector3(targetX, targetY, _targetPosition.z);
// Set Velocity
_velocity = new Vector3(mouseInputX, mouseInputY);
}
// Update Position to target position
movingObjectTransform.localPosition = _targetPosition;
}
if (Input.GetMouseButtonUp(mouseButton)) // Mouse Button Released
{
// Clean up old tween if it exists
if (_tween != null && _tween.IsActive())
{
_tween.Kill();
}
// If we have movement, then we add inertia
if (_velocity.sqrMagnitude > 0)
{
// Tween our inertia
_tween = movingObjectTransform.DOLocalMove(_targetPosition + (_velocity * inertiaAmount), inertiaDuration)
.SetEase(easeType).OnUpdate(UpdateTargetPosition);
// Update our target position to the new tween position
void UpdateTargetPosition() => _targetPosition = movingObjectTransform.localPosition;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment