Entity parenting help

I’m parenting an entity (Y) to another entity (X) upon Entity:Touch

Here’s what I’m gonna do

  1. Get the pos of X.
  2. Remove Y and instantly spawn a Z in exact same pos and parent Z to the X.
  3. Making sure Z sticks to X at all times
  4. Have X control the animation stage of Z.

Now how do I.

  1. Spawn Z, then instantly attach it to X, rotate it’s angle so it’s 90 degrees to X.
  2. Make sure that Z is always the same pos to X but can still be moved around with grav gun if X is grav gunned.
  3. Make the parent control the animation stage of the children named Z, like idle, spin1, spin2 and spin3.

In your Entity:Touch( entY ) function :

You will have to use :

local entZ = ent.Create("some_ent")
entZ:SetPos( entY:GetPos() )
entZ:SetMoveType( MOVETYPE_NONE ) 
entZ:SetParent(self) -- assuming we are in entX functions

But having MOVETYPE_NONE means it cannot move at all right? I want it to move if entX is moved, like it’s glued to it

MOVETYPE_NONE just means no physics will be applied to it.

So I’d have to check if entX is still the same pos relative to entY every frame and if it arent set its pos? Expensive

Well, that’s ridiculous, why would you have to check pos while both entities are linked ?
enough spoon-feeding here… See the goal of SetParent function…

When you parent an entity I believe it positions itself on the parented entity itself. Along with that, the child entity will have a localized position to the parent entity, rather than being global. At least that’s how a lot of engines work.

In GMod it’s like welding these two entities. If you move the first entity, the second will move exactly the same way.
Both entities still have global positions ( of course they are still there in the 3D space) but the child entity will move with the parented entity so the local position of the child entity (relative to parent entity) will stay the same.

@Klaes4Zaugen The best way to learn is to try it yourself, first try to parent both entities you created ( it can be physic_props ) and then testing some functions, moving them around, try to discover and imagine how you could use it in your script

  • snip, WorldToLocal is noice