- Home /
put the ring in the pole by sorting order from largest to smallest.
(when i put all the rings in the pole the gameobject will be locked. how to unlock it and back to its initial position)
using System.Collections; using System.Collections.Generic; using UnityEngine; using System;
public class vRingLevel2Drag : MonoBehaviour { public static event Action PuzzleDone = delegate { }; [SerializeField] private Transform standPosition = default; private Vector2 initialPosition; private Renderer rend; private float deltaX, deltaY; private bool moveAlowed; private bool locked;
private void Start()
{
initialPosition = transform.position;
rend = GetComponent<SpriteRenderer>();
}
private void Update()
{
if (Input.touchCount > 0 && !locked)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
{
moveAlowed = true;
rend.sortingOrder = 3;
deltaX = touchPos.x - transform.position.x;
deltaY = touchPos.y - transform.position.y;
}
break;
case TouchPhase.Moved:
if (moveAlowed)
transform.position = new Vector2(touchPos.x - deltaX, touchPos.y - deltaY);
break;
case TouchPhase.Ended:
moveAlowed = false;
rend.sortingOrder = 2;
if (Mathf.Abs(transform.position.x - standPosition.position.x) <= 1f &&
Mathf.Abs(transform.position.y - standPosition.position.y) <= 5f)
{
switch (vRingLevel2GameControl.slotsOccupied)
{
case 0:
transform.position = new Vector2(standPosition.position.x, -4.01f);
vRingLevel2GameControl.slotsOccupied = 1;
break;
case 1:
transform.position = new Vector2(standPosition.position.x, -2.93f);
vRingLevel2GameControl.slotsOccupied = 2;
break;
case 2:
transform.position = new Vector2(standPosition.position.x, -1.89f);
vRingLevel2GameControl.slotsOccupied = 3;
break;
case 3:
transform.position = new Vector2(standPosition.position.x, -0.83f);
vRingLevel2GameControl.slotsOccupied = 4;
break;
case 4:
transform.position = new Vector2(standPosition.position.x, 0.26f);
vRingLevel2GameControl.slotsOccupied = 5;
break;
case 5:
transform.position = new Vector2(standPosition.position.x, 1.31f);
PuzzleDone();
break;
}
locked = true;
}
else
{
transform.position = new Vector2(initialPosition.x, initialPosition.y);
}
break;
}
}
}
}
Here's my game control:
public void CheckResults()
{
if (rings[0].position.y == 1.31f &&
rings[1].position.y == 0.26f &&
rings[2].position.y == -0.83f &&
rings[3].position.y == -1.89f &&
rings[4].position.y == -2.93f &&
rings[5].position.y == -4.01f)
{
EndGame();
}
else
{
TryAgain();
vRingLevel2Drag.PuzzleDone -= CheckResults;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613014751im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
mouse click obj to initiate action, then locking the obj so action cant be repeated 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Who can help me with lock/unlock and hint/coin systems? 0 Answers
How do I enable the Android lock screen in developer mode? 1 Answer
make game objects not move in scene view 2 Answers