I'm having an issue getting two separate objects to stick together.

Hello,

So I’ve had an issue with getting two objects to stick together,

in the game, the player should be able to connect with a prefab game object as well as disconnect from it and both the player and the object are moving.

So far I’ve attempted getting the player’s rigid body transform to be the same as the game object it comes in contact with which didn’t really work since for me to reference one or the other would mean one would have to be static and therefore not moving which doesn’t work for the application here.

I’ve tried setting the player’s rigid body’s velocity to be the same as the other object, however, the player ends up moving faster than the object and isn’t necessarily connected with the object.

The issue I think I’m having with this and I’m not really sure how to get around is that each prefab is using the same C# script so I can’t just parent the player to all the objects. In addition, which objects the player connects with depends on which one they run into.

I’m not sure if it’s possible but what I think would work best is if I could set the distance between the two objects to be a set distance. so then it would only depend on the object the player runs into and it would stay at that distance until the player disconnects from the object.

I’ve been attempting to make this work for a couple of days now and I’m very open to other suggestions.

You lost me here. You can reference anything you want and they don’t have to be static; and static does not mean that it will not move in terms of C# because static just means it’s independent.

Anyway the easiest way is I believe would work is moving the follower object to be a child of the leader and disallow it’s velocity but you should also be able to keep them independent then follower.velocity += leader’s velocity.

I was getting an error with my non-static references then and I’m not sure how to reference them in a non-static way. Whenever I attempted to use the rigid bodies they would say they are read-only. Like I think they should be able to be referenced considering neither of them was created as static rigid bodies.

I understand what you’re saying that they can just be static in code, but when I make either static in code they are then unable to move in the game.

In addition there are multiple different objects the player (the follower) can follow. So I don’t know of a way to make the player child to multiple different objects at the same time.

followerGameObject.transform.parent  = leaderGameObject.transform;

You would just do it and undo as needed by making the follower parent to something else.