Camera vertically scrolling effect (scroll out)
I want to create a camera movement like idle miner tycoon. So the Camera should scroll vertically by panning with a typically scroll effect.
This is my code:
Vector3 touchStart;
public int upperLimit = 0;
public int lowerLimit = 7000;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
touchStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
if (Input.GetMouseButton(0))
{
Vector3 direction = touchStart - Camera.main.ScreenToWorldPoint(Input.mousePosition);
float finalYPos = Camera.main.transform.position.y + direction.y;
finalYPos = Mathf.Clamp(finalYPos, lowerLimit, upperLimit);
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, finalYPos, Camera.main.transform.position.z);
Debug.Log(finalYPos);
}
}
But how can I make that the scrolling is ran out. Actually the movement is stopping directly when I leave the mouse/thump.
Thank you for your help
Best regards
Comment
Answer by jarleyinc · Jun 14, 2021 at 09:23 PM
I found a Solution for everybody, who has the same problem:
[SerializeField]
private Camera cam;
Vector3 touchStart;
private float topLimit = 0f;
private float bottomLimit = -2000.0f;
private Vector3 _curPosition;
private Vector3 _velocity;
private bool _underInertia;
private float _time = 0.0f;
public float SmoothTime = 2;
public Vector3 direction;
void Update()
{
PanCamera();
if (_underInertia && _time <= SmoothTime)
{
cam.transform.position += _velocity;
float newY = Mathf.Clamp(cam.transform.position.y, bottomLimit, topLimit);
cam.transform.position = new Vector3(cam.transform.position.x, newY, cam.transform.position.z);
_velocity = Vector3.Lerp(_velocity, Vector3.zero, _time);
_time += Time.smoothDeltaTime;
}
else
{
_underInertia = false;
_time = 0.0f;
}
}
private void PanCamera()
{
if (Input.GetMouseButtonDown(0))
{
touchStart = cam.ScreenToWorldPoint(Input.mousePosition);
_underInertia = false;
}
if (Input.GetMouseButton(0))
{
Vector3 _prevPosition = _curPosition;
direction = touchStart - cam.ScreenToWorldPoint(Input.mousePosition);
float finalYPos = cam.transform.position.y + direction.y;
Debug.Log("Old: " + finalYPos);
finalYPos = Mathf.Clamp(finalYPos, bottomLimit, topLimit);
Debug.Log("New: " + finalYPos);
Vector3 desiredPosition = new Vector3(cam.transform.position.x, finalYPos, cam.transform.position.z);
cam.transform.position = desiredPosition;
_curPosition = desiredPosition;
_velocity = _curPosition - _prevPosition;
}
if (Input.GetMouseButtonUp(0))
{
_underInertia = true;
}
}
Your answer
Follow this Question
Related Questions
I want to make my camera follow the player ONLY when the player is on solid ground [2D] 1 Answer
Draw on a Plane surface with varying camera positions 1 Answer
Camera is making trail in moving objects 0 Answers
Pixel jitter/flicker while camera movement (2D) 2 Answers
Camera Bounds in Complex Shape 1 Answer