I
I
ILIIA22892020-06-05 17:16:32
C++ / C#
ILIIA2289, 2020-06-05 17:16:32

Camera swipe with unity swipe?

Hello everyone, I have a script for moving the camera horizontally with swipes. But it does not work. The camera is orfografic. Swipes should work both on the PC and on the phone

using UnityEngine;
public class CameraSwipe : MonoBehaviour{

    private Vector2 startPos;
    private Camera cam;
    private float targetPos;
    public float speed;
    public float minX;
    public float maxX;

    void Start(){
        cam = GetComponent<Camera>();
        targetPos = transform.position.x;
    }

    void Update(){
        if (Input.GetMouseButtonDown(0)) startPos = cam.ScreenToWorldPoint(Input.mousePosition);
        else if(Input.GetMouseButtonDown(0))
        {
            float pos = cam.ScreenToWorldPoint(Input.mousePosition).x - startPos.x;
            targetPos = Mathf.Clamp(transform.position.x - pos, minX, maxX);
        }

        transform.position = new Vector3(Mathf.Lerp(transform.position.x, targetPos,speed * Time.deltaTime),transform.position.y,transform.position.z);
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question