Search Unity

making one game object follow another

Discussion in '2D' started by Mark_Wilson, Jul 19, 2014.

  1. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Hi guys, In my game I am trying to get an elephant to follow the player elephant when a pop up button is pressed.

    So basically, I want the trunk of the collected elephant to lock on to the player elephant and then when the player moves, the collected elephant will walk behind the player.

    I'm having a lot of problems getting this to work and the current way I am trying this is getting quite complicated.

    Could anyone either show me how to make my current idea work or point out another easier way of doing it.

    Here is my code. -
    Code (CSharp):
    1. if (collider2D == Physics2D.OverlapPoint(touchPos))
    2.             {
    3.        
    4.                 Destroy (gameObject); //destroy the connect button
    5.                 Vector3 temp = GameObject.FindWithTag ("el2Trunk").transform.position; //send the elephant to the back of the player
    6.                 temp.x = joinElephants.playerXPos;
    7.                 GameObject.FindWithTag ("el2Trunk").transform.position = temp;
    8.  
    9.             }
    10.         }
    11.  
    Okay, so whats happening here is, there is a pop up button which is a child of the collectable elephant, when this is clicked, the button is destroyed and It detects a separate game object with the tag - "el2Trunk" This game object is a child of the collectable elephant.
    I then set the x value of that game object to share the same x value of a completely separate game object which is part of the player character. to do this I have set up a public var in my button script and dragged a game object from the player parent into the public var slot, which allows me to access it's variables and stuff.

    I hope that makes sense guys, this currently doesn't work at all but gives me no errors.
    Any help would be much appreciated as i'm really stuck right now.
    all I want to do is make one object share the same x value as another but this turns out to be huge challenge for some reason.

    Cheers guys,

    Mark
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Remind me again why you're not using a HingeJoint2D? Or, changing the parentage if you don't want it to rotate?
     
  3. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    I briefly looked at hinge joint 2d but according to another user it wasn't appropriate. And I don't know how to change to parentage or that you even could!
    I'm assuming you're telling me that's the answer?

    Cheers,
    Mark
     
  4. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Yeah, you can totally just use:

    transform.parent = GameObject.FindWithTag ("el2Trunk").transform;

    That will definitely cause them to move in lockstep - anything that happens to the parent, will be applied to the child. The disadvantage is that it will look very stiff, while a HingeJoint2D would let the elephant rotate freely, but requires physics.
     
  5. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Ah okay, I thought it would work but it doesnt seem to be doing anything.
    I have a lot of objects, scripts and child objects all interlinked so i must have a wrong connection somewhere.
    I'll do more research on HingeJoint2d though, as you say, the way i'm doing it might cause complications when my character flips its direction of travel and climbs uphill and stuff.
    my elephants are both rigid bodies so they react to physics.

    Thanks for the advice :)

    Cheers,
    Mark
     
  6. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Ooops, you'll have to disable that rigidbody if you want the parentage to kick in.

    Seriously, if they're all physics objects, go with a HingeJoint2D.
     
  7. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    Ah, you mean i'll need to disable the rigid body if I want to use hinge2d?
    What If I make an empty gameobject on both sprites, so that way its only a child of a rigid body object. would that be okay?