using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class no1 : MonoBehaviour
{
  [SerializeField]
    private Transform no1bg;
    private Vector2 initialPosition;
    private float deltaX,deltaY;
    public static bool locked;

    
    // Start is called before the first frame update
    void Start()
    {
      initialPosition = transform.position;  
      Application.targetFrameRate = 60;
    }

    // Update is called once per frame
    private void Update()
    {
      if (Input.touchCount > 0 && !locked) // jika terdapat input touch lebih dari 0 dan angka belum terlock
      {
          Touch touch = Input.GetTouch(0); // maka mendapat statement untuk touch 
          Vector2 touchpos = Camera.main.ScreenToWorldPoint(touch.position);

          switch (touch.phase)
          {
            case TouchPhase.Began: 
                if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchpos))
                {
                  deltaX = touchpos.x - transform.position.x;
                  deltaY = touchpos.y - transform.position.y;  
                }
                break;

            case TouchPhase.Moved:
                if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchpos))
                transform.position = new Vector2(touchpos.x - deltaX, touchpos.y - deltaY); 
                break; 

             case TouchPhase.Ended:
             if (Mathf.Abs(transform.position.x - no1bg.position.x) <= 0.5f &&
                Mathf.Abs(transform.position.y - no1bg.position.y) <= 0.5f)
             {
              transform.position = new Vector2(no1bg.position.x, no1bg.position.y);
              locked=true;   
             }
             else{
                 transform.position = new Vector2(initialPosition.x, initialPosition.y);
             }
             break; 
          }
      }  
    }
}

so i have this switch case for an object so when that object is locked it will activate a certain cutscene. It went well until i load the next scene and went back to the previous scene... The object is stuck in a locked position... How do i make that object return to normal like go back to the TouchPhase.Began instead of staying in TouchPhase.Ended when i load to another scene?

0

There are 0 best solutions below