Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 $$anonymous$$ · Jul 09, 2019 at 07:22 PM · 2dcollisioncollision2dmouse-dragcollision issues

How to move objects away from each other (not what you think)

I am making a game where I can drag objects around, but they can't go far inside each other, so I made it so that the dragging function doesn't work even if they are slightly inside. Then, in order for the drag function to work again, the objects need to move away from each other. This is where I have problems. The parts of the code I am talking about are in the onmousedrag method, so everything only works if the mouse is held down. This is why neither Vector2.movetowards nor transform.Translate work. The objects also can't have rigidbodies so adding a force doesn't work. I need help. The game is 2D.

  private void OnMouseDrag()
  {
      if (snapHall == null) return;
      HallwayBehavior otherHall = snapHall.GetComponent<HallwayBehavior>();
 
      Vector2 pos = transform.position;
      Vector2 snapHallPos = snapHall.transform.position;
 
      float xdiff = Mathf.Abs(pos.x - snapHallPos.x);
      float ydiff = Mathf.Abs(pos.y - snapHallPos.y);
 
      int z = Mathf.Abs(Mathf.RoundToInt(rot.z));
      int otherZ = Mathf.Abs(Mathf.RoundToInt(otherHall.rot.z)); //the vector rot is basically the rotation of the gameobject
 
      bool inside = false;
 
      if(  (z == 180 || z == 0) && (otherZ == 180 || otherZ == 0)  )
      {
          if (xdiff < 6 && ydiff < 2.5f)
          {
              inside = true;
          }
          else inside = false;
      }       
 
      if (!inside)
      {
          Vector3 newPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
          transform.position = Camera.main.ScreenToWorldPoint(newPosition) + offset; //These two lines are for dragging
      }
      else
      {
          //this is when the object needs to move away
      }
  }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by I_Am_Err00r · Jul 09, 2019 at 08:07 PM

Ok, I'm sorry, I don't know what is what with those variable names.

You will need a couple of variables for this script:

 Vector3 lastObjectPosition;
 Collider2D lastObjectCollider;
 GameObject currentObject;


Then when you grab an object, you change the lastObjectPosition to something like this:

 currentObject = WhateverObjectYouSelectedWithMouse
 lastObjectCollider = currentObject.GetComponent<Collider2D>();
 lastObjectPosition = new Vector 3 ((currentObject.transform.position.x - lastObjectCollider.size.x), currentObject.transform.position.y, currentObject.transform.position.z);

then when collect a new object, you just run that code again.

this should snap the object directly behind the one that is in last.

Sorry if I didn't explain it well or it doesn't work like you need it to.

Comment
Add comment · Show 1 · Share
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 $$anonymous$$ · Jul 09, 2019 at 08:10 PM 0
Share

Lol sorry abt the variables, I needed many. This should work, thanks alot!

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

250 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to make a little wiggle room for the collision? 1 Answer

Best way to manually calculate collision between GameObjects and a Tilemap without using physics 1 Answer

Unity2D: How to make my player not walk through walls? 1 Answer

Colliders at wrong position after building the game? 1 Answer

Different platform effector 2d rotations for different colliders 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