Search Unity

Keeping Papers Together with a Staple

Discussion in 'Scripting' started by artistshc, Jul 27, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Hi. I have a stapler and when you double click it, it instantiates a staple. If it is on papers it staples them together. It isn't working too well. It depends on where you click on whether the papers stay together. Is there a way to make it so no matter where you click on the papers or the staple, they still stay together?

    This is the code on the staple

    Code (CSharp):
    1.     //STAPLE COLLIDING WITH OTHER OBJECTS
    2.     void OnTriggerEnter2D(Collider2D other) {
    3.         //if other object is paper then....
    4.         if (other.gameObject.name.Contains ("Paper")) {
    5.             //change position of the papers to the staple's position
    6.             other.transform.position = this.transform.position;
    7.             other.transform.parent = this.transform;
    8.  
    9.         }
    10.     }
    11.     //STAPLE COLLIDING WITH OTHER OBJECTS
    12.     void OnTriggerExit2D(Collider2D other) {
    13.  
    14.         if (other.gameObject.name.Contains ("Paper")) {
    15. //changes position of staple to papers position - for some reason this code helps keep them together
    16.             gameObject.transform.position = other.transform.position;
    17.             gameObject.transform.parent = other.transform;
    18.         }
    19.     }
     
  2. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Any suggestions?
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,655
    Is there a reason to change the position? Based on my understanding of the problem, the staple is being instantiated into a position that, if correct, will collide with the paper object's collider and trigger this function. At this point, the staple is in the right position (it's defined as the right position, otherwise it wouldn't collide), and the paper is in the right position, so why's there a need to move the staple to the paper's personal origin point or vice-versa? Just change the parent and then any further movement/rotation/whatever will be tied to the parent, while the relative position between them will remain the same as it was when it was "stapled".

    If this is incorrect, please provide more details on what exactly it is that you want to happen.

    Also, in the future if you need to change the position / rotation to match a parent, you change the parent first then you zero out the child's "local position" (or change it to some relative value).
     
    artistshc likes this.
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Dear Lysander. Thank you so much for answering. <3 I was getting nervous nobody would help. I really appreciate your kindness! I took the position out and it works a bit better. Could you please change the code to what you think it should be...if it is only 2 lines or so. I'd greatly appreciate it. I feel so stupid! eek. Thank you so much!
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,655
    Code (csharp):
    1. gameObject.transform.parent = other.transform;
    2. gameObject.transform.rotation = other.rotation;
    3. gameObject.transform.localPosition = Vector3.zero;
    You can also define some sort of offset that matches the staple up to the paper better, for instance:
    Code (csharp):
    1. gameObject.transform.parent = other.transform;
    2. gameObject.transform.rotation = other.rotation;
    3. gameObject.transform.localPosition = new Vector3(-5, 10, 0);
    If you want to be cute AND make things easier for yourself in the long run, create a new Staple instance in the scene (break the link to the prefab immediately) and make it a child of a Paper instance (make it a part of the paper prefab). Line it up exactly how you'll want the staples you'll instantiate to be lined up and such, the right rotation and scale and all of that, then delete everything except the Transform so it's a completely empty GameObject with nothing but relative-position/rotation data. Call it "StapleTarget" or something.

    When you go to do the staple+paper collision, you can use that "StapleTarget" child object's transform and copy it all over to the current staple (after you've changed your parent to the paper). This will be a bit easier if you make a Transform public member on the Paper GameObject and then drag the Target into it, so the Staple can access it without doing a child search later by just using GetComponent for the script it's saved in on the Paper. This method has the added benefit of you being able to change where the staple will go later just by using the inspector.
     
    artistshc likes this.
  6. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    WOW!!! Thank you soooooooooooooo much! It works perfectly! I really, really, really, really appreciate your help! Thank you and have a GREAT day!!! <3
     
unityunity