Search Unity

Trying to attach a sprite to another sprite

Discussion in '2D' started by heroice18, Aug 3, 2019.

  1. heroice18

    heroice18

    Joined:
    Jul 27, 2019
    Posts:
    2
    Currently I'm trying to take the plug object I have and have it "plug" into the terminals. I can drag it with my mouse into one of those terminals and stay there until the user decides to move it to another terminal. Should I use joints? How do I make sure that it stays attached to the sprite that I want?


     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Do you have anything working yet?

    A very simple way to do this would be to add a new gameobject as a child to each terminal which will represent the "socket" where the plug should be. Position the socket where the plug should be when its plugged in. Then when the plug overlaps a socket, set the plug to the socket's position and stop the user's drag action.
     
    vakabaka likes this.
  3. heroice18

    heroice18

    Joined:
    Jul 27, 2019
    Posts:
    2
    Yes the plug can move around on screen, I'm trying to set up the terminals to accept the plug and have the plug stay there. One of the problems is that the rigid body;s 2D Physics has the plug fall down to the bottom of the screen rather than stay where the mouse went.
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    you can set gravity scale to zero (rigidbody2d component) or maybe use kinematic rigidbody

    and as jeffreyschoch said, child can help. One way, you can make the plug child from the terminals if it is on the needed place (also you can disable rigidbody and collider at this moment). Other way, you can make sprite from the pluged object, place it at needed position and disable. Then when the plug was moved, you can disable it and in the moment enable the disabled sprite from the plug. And I am sure there are many other ways.

    You can check if the plug is on the needed position with OnTriggerEnter2D.
     
    Last edited: Aug 3, 2019