• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Tubestorm · Nov 14, 2021 at 06:41 AM · iosunity 2dcamera-movementmobile devicessmoothing

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;
         }
 
     }
 
 }
 




Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I prevent users from skipping video by leaving the app and coming back? 0 Answers

How to make Unity 4.3 + RageTools+ RageSpline animations and how to handle them in Xcode? 0 Answers

Trouble with logic related to my camera smoothing 0 Answers

Unity 3D Scene loading Issue in iOS Build 2 Answers

How do I properly set up touch controls for First-Person view. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges