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. -
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
Destroy (gameObject); //destroy the connect button
Vector3 temp = GameObject.FindWithTag ("el2Trunk").transform.position; //send the elephant to the back of the player
temp.x = joinElephants.playerXPos;
GameObject.FindWithTag ("el2Trunk").transform.position = temp;
}
}
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.
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?
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.
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.
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?