Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 pk010302 · Aug 26, 2019 at 12:29 PM · collision detectioncollision2ddraggingdrag and dropdrag objects

Dragged Object, When it Collides with Another Object, Will Go Back to Initial Position

Hey guys! I'm making a drag-and-drop, object matching puzzle game with some moving obstacles and a countdown timer to make it a bit interesting. Anyway, what I want to happen is that when the dragged object collides with the obstacle (both colliders have kinematic rigidbodies), it will go back to the initial position and the timer will continue running.

Well, the collision kinda works but only if I restart the level, which means that the timer will start at the beginning. I tried doing transform.position = initialPosition to the draggable object but it renders the collision useless (basically, it won't "collide" with the obstacle). I'm wondering if someone can help me with this. The codes are posted below.

Draggable Object

 public class Hexagon : MonoBehaviour
 {
     public Transform HexagonGoal;
     public float GoalOffset = 0.5f;
 
     Vector2 initialPosition;
     Vector2 mousePosition;
     float xOffset, yOffset;
 
     [HideInInspector]
     public static bool Locked;
 
     // Start is called before the first frame update
     void Start()
     {
         initialPosition = transform.position;
     }
 
     void OnMouseDown()
     {
         if (!Locked)
         {
             xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
             yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
         }
     }
 
     void OnMouseDrag()
     {
         if (!Locked)
         {
             mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             transform.position = new Vector2(mousePosition.x - xOffset, mousePosition.y - yOffset);
         }
     }
 
     void OnMouseUp()
     {
         if (Mathf.Abs(transform.position.x - HexagonGoal.position.x) <= GoalOffset &&
             Mathf.Abs(transform.position.y - HexagonGoal.position.y) <= GoalOffset)
         {
             transform.position = new Vector2(HexagonGoal.position.x, HexagonGoal.position.y);
             Locked = true;
         }
         else
             transform.position = initialPosition;
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Trap" && !Locked)
         {
             //transform.position = initialPosition;
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         }
     }
 }

Snippet of the GameController, which has the timer

 // Update is called once per frame
     void Update()
     {
         if (timerActive)
         {
             currentTime -= Time.deltaTime;
             TimerText.text = currentTime.ToString("0");
 
             if (currentTime <= 0)
             {
                 currentTime = 0;
                 timerActive = false;
             }
 
             if (Hexagon.Locked)
             {
                 timerActive = false;
                 QuestionCanvas.SetActive(true);
                 GameCanvas.SetActive(false);
              }
         }
     }
Comment
Add comment · Show 3
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 Glurth · Aug 26, 2019 at 04:06 PM 0
Share

The transform.position = initialPosition; line certainly looks like it SHOULD work, assumpting the scene and its objects are configured correctly (and you un-remark it, and remove the loadscene line). I would suggest adding a

 Debug.Log("Resetting position of object "+gameObject.name +"  from:"+transform.position+"  to:"+ initialPosition);

right before you assign the initialPosition to the transform. Just to make sure it is being called when you expect, and using the values you expect (sanity-check). Edit: or as a double sanity check.. display the transform position, AFTER you assign it also.

avatar image pk010302 Glurth · Aug 27, 2019 at 04:35 AM 0
Share

The Debug.Log thing certainly works, as it shows the values where it should go back to. However, the game object doesn't return to where it should return. I think it may have something to do with On$$anonymous$$ouseDown() or On$$anonymous$$ouseDrag(). I'm thinking of making another bool for those functions to check if it hits the obstacle then see how it goes.

Edit: Here's what happened, the game object does return to the initial position when it hits the obstacle but then it instantly goes back to where my cursor is. Here are the changes I made:

     // Reference to the traps that exist in the scene
     public Transform[] Traps;
 
     void On$$anonymous$$ouseDown()
     {
         //if (!Locked)
         //{
         //    xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
         //    yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
         //}
 
         foreach (var trap in Traps)
         {
             if (!Locked)
             {
                 if ($$anonymous$$athf.Abs(transform.position.x - trap.position.x) <= TrapOffset &&
                     $$anonymous$$athf.Abs(transform.position.y - trap.position.y) <= TrapOffset)
                 {
                     transform.position = initialPosition;
                 }
                 else
                 {
                     xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
                     yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
                 }
             }
         }
     }
 
     void On$$anonymous$$ouseDown()
     {
         //if (!Locked)
         //{
         //    xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
         //    yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
         //}
 
         foreach (var trap in Traps)
         {
             if (!Locked)
             {
                 if ($$anonymous$$athf.Abs(transform.position.x - trap.position.x) <= TrapOffset &&
                     $$anonymous$$athf.Abs(transform.position.y - trap.position.y) <= TrapOffset)
                 {
                     transform.position = initialPosition;
                 }
                 else
                 {
                     xOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;
                     yOffset = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;
                 }
             }
         }
     }
avatar image Glurth · Sep 02, 2019 at 03:31 PM 0
Share

Think I see the problem, not sure tho.... When you set the transform.position to the initialPosition, in OnTriggerEnter2D(): you ALSO need to reset the xOffset and yOffset variables, like you do in On$$anonymous$$ouseDown().

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

112 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

Related Questions

Collision while dragging in Canvas with IDragHandler 0 Answers

Following instruction for drag and drop 3d object unity but not working 1 Answer

spawn and drag game object without leaving mouse button 1 Answer

if statement not working when detecting collision between two prefabs 1 Answer

2D Collider seems bigger than it is 1 Answer


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