- Home /
Camera Smoothing from pos A - pos B in Unity 2D
Hello,
I'm working on a 2D game, something similar to Animal Restaurant and Cat spa, (for reference.)
I'm trying to incorporate a camera movement, so the camera moves to a position based on swipe direction.
MY PROBLEM:
Right now the lerping doesn't smooth the transition and the camera still snaps to the new position. I was hoping to have the transition to the new position smoother.
Thanks so much for you time.
Here is the code that detects the swipe direction and moves the camera. The position i want the camera to go to is saved as a vector 3 and serialized so I can edit it in the inspector.
Line 90 is where the problem is.
using System.Collections;
using UnityEngine;
public class SwipeDetection : MonoBehaviour
{
[SerializeField]
private float minimumDistance = 0.2f;
[SerializeField]
private float maximumTime = 1f;
[SerializeField, Range(0f, 1f)]
private float directionThreshold = 0.9f;
[Header("Camera Zone Position")]
[SerializeField]
private Vector3 cameraPositionZone1 = new Vector3(); //0f, 0f, -10f
[SerializeField]
private Vector3 cameraPositionZone2 = new Vector3(); //4.1416f, 0f, 10f
[SerializeField]
private float smoothSpeed = 10.0f;
private InputManager inputManager;
private Vector2 startPosition;
private float startTime;
private Vector2 endPosition;
private float endTime;
//SWIPE DIRECTION BOOL
private bool swipeUp = false;
private bool swipeDown = false;
private bool swipeRight = false;
private bool swipeLeft = false;
private Camera cameraMain;
private void Awake()
{
inputManager = InputManager.Instance;
cameraMain = Camera.main;
}
private void OnEnable()
{
inputManager.OnStartTouch += SwipeStart;
inputManager.OnEndTouch += SwipeEnd;
}
private void OnDisable()
{
inputManager.OnStartTouch -= SwipeStart;
inputManager.OnEndTouch -= SwipeEnd;
}
private void SwipeStart(Vector2 position, float time)
{
startPosition = position;
startTime = time;
}
private void SwipeEnd(Vector2 position, float time)
{
endPosition = position;
endTime = time;
DetectSwipe();
}
private void DetectSwipe()
{
//measure distance between end pos and start pos of swipe
if(Vector3.Distance(startPosition, endPosition) >= minimumDistance && (endTime - startTime) <= maximumTime)
{
Debug.Log("Swipe Detected");
Debug.DrawLine(startPosition, endPosition, Color.blue, 5f);
Vector3 direction = endPosition - startPosition;
Vector2 direction2D = new Vector2(direction.x, direction.y).normalized; //normalises vector result from 0-1 instead of 0-100?
SwipeDirection(direction2D);
if ((cameraMain.transform.position == cameraPositionZone1) && swipeRight == true) //if you are in zone1 and you swipe right = nothing will happen
{
Debug.Log("No more zones, try swiping left");
//do nothing for now
}
if ((cameraMain.transform.position == cameraPositionZone1) && swipeLeft == true) //if you are in zone1 and you swipe left = move camera to zone2
{
//HERE LIES THE MAIN PROBLEM
cameraMain.transform.position = Vector3.Lerp(cameraMain.transform.position, cameraPositionZone2, Time.deltaTime * smoothSpeed);
}
}
}
private void SwipeDirection(Vector2 direction)
{
//dot product to determine if it is going in the towards eachother 1, opposite -1, perpendicular 0
//Here we check what direction the player is swiping using Unity Dot Product
if(Vector2.Dot(Vector2.up, direction) > directionThreshold)
{
Debug.Log("Swipe Up");
swipeUp = true;
}
else if (Vector2.Dot(Vector2.down, direction) > directionThreshold)
{
Debug.Log("Swipe Down");
swipeDown = true;
}
else if (Vector2.Dot(Vector2.left, direction) > directionThreshold)
{
Debug.Log("Swipe Left");
swipeLeft = true;
}
else if (Vector2.Dot(Vector2.right, direction) > directionThreshold)
{
Debug.Log("Swipe Right");
swipeRight = true;
}
}
}
Your answer
Follow this Question
Related Questions
Follow camera in 2d game 2 Answers
Patcher for an iOS Unity 3D game 0 Answers
Multipurpose CameraRig - Smooth Movement - how to achieve? 1 Answer