• 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
0
Question by charleneconcha · Oct 29, 2020 at 06:12 AM · gameobjectlockunlock

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

}

Comment
Add comment · Show 1
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
avatar image charleneconcha · Oct 28, 2020 at 09:05 PM 0
Share

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

 

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

215 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 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

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

Locking and Unlocking a cursor... 2 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