Download

Socket and physics issue

Hey Unreal forums, I’m currently working with sockets for the first time but I have a few issues that I hope someone could shed some light upon for me.

What I’m doing:

  1. Player locates bag
  2. Presses E key to pickup
  3. Presses G Key to throw down

Issues I have:

  • If I enable physics on the actor the bag item will state it is picked up but will not change locations
  • If I attempt to enable physics on drop (after detachment is called) the game engine will crash
  • Despite naming the socket I have the issue that the bag will attach to the very center (probably the root bone)

Code that I use:


void ACP_Character::ThrowBagAction()
{

	if(!hasBag)
	{
		hasBag = true;
		//const FDetachmentTransformRules detachRules(EDetachmentRule::KeepWorld, false); MAY NOT NEED
		heldLootBag->DetachRootComponentFromParent(true);
		//heldLootBag->GetRootPrimitiveComponent()->SetSimulatePhysics(true); CRASHES THE GAME
		heldLootBag = nullptr;

	}

}


void ACP_Character::BeginUseAction()
{

	if(currentLootBag)
	{

		if(hasBag)
		{

     			hasBag = false;        
			FName socketName = TEXT("BagBone");
			//currentLootBag->GetRootPrimitiveComponent()->SetSimulatePhysics(false); STILL WONT PICKUP IF PHYSICS IS ENABLED PRIOR TO ATTACHMENT
			const FAttachmentTransformRules attachmentRules(EAttachmentRule::SnapToTarget, true);
			currentLootBag->AttachToActor(this, attachmentRules, socketName);
			heldLootBag = currentLootBag;
			return;

		}

	}
}

Relevant images of the work:

Socket name/preview
W58Y2lz.png

Attachement location using current code
ufhK11U.png

Example showing if physics is enabled that the bag location doesn’t move with the same code (Check right side for confirmation that it’s being picked up but not moved)
MsqsR6C.png

Last words:

Thank you to anyone who can help me with this, I appreciate anytime given.

I think you want to use DetachFromActor on the bag actor, not DetachRootComponentFromParent. I think the latter is giving you an empty actor with no root object and a component with no actor. You should also be able to uncomment your SetSimulatePhysics line too (make sure you also enable collision first, or the component will ignore your request).

Thank you for the response, I’ve taken your ideas into practice and found that it makes no difference, I still recieve an engine crash on setting physics to true after detachment and even though I’ve confirmed the existance of the socket it will still crotch rocket everything

You should probably ensure that heldLootBag is not null, the same way you do with currentLootBag. Also, I’m not sure about how your code is supposed to work, but should the first code block have “if (hasBag)” and not “if (!hasBag)” (I never see you set it to true tho)? Also, I think you need to use AttachToComponent and specify the Character’s mesh, not AttachToActor. The root component of a Character is the capsule, not the mesh. Your code is probably trying to attach the bag to a non-existent socket on the Capsule.

You are right about the hasBag, for some reason I omitted it and didn’t even check over it, stupid of myself but I am still struggling with the attachement, if it is attaching to the capsule and not the mesh socket then I’m unsure what function to call to get it to attach to the mesh seeing as some of the older API complains with the compiler that it’ll soon be made depracted and the only API I can find in the engine now seems to only take in an AActor, no overload for a mesh.

*Edit:

I’ve realized I can use polymorphism with GetMesh() and the function that takes a USceneComponent but the same still occurs, it is not locating the Socket but If I Independantly search for the socket instead of trying to attach to it, it works.

So you have something like this:



currentLootBag->AttachToComponent(GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, "BagBone");


?

You may also need to disable physics on the bag before you attach it, then re-enable it when throwing/dropping it. I’m not sure if Unreal allows you to attach physics simulated objects to anything and I don’t think you need physics on the bag if the character is holding it (it should just be aligned to the socket you’ve created). Of course if you need the bag to pivot in the character’s hand with physics forces, I think you’ll have to use a physics constraint or physics handle component.

Well I realized a few inconsistencies again so I did an editor restart, it’s working fine now, at some point the engine decided that it didn’t want to provide updates anymore.

Thank you for your time and may this engine be fixed before it kills me.

Ah, sorry to hear that - I constantly have to restart the editor after making C++ changes. If something I’m coding doesn’t seem to be working like I think it should, I restart the editor before debugging further, because it can be a huge waste of time otherwise :frowning: In fact, I have to restart the editor after adding any properties to my C++ classes, or the editor/game will crash during multiplayer testing.

Instead of making a second topic, would you know why if there are physics enabled on the actor that it won’t move and attach to the socket?
If I have physics enabled then disable them before attaching I just get a fatal engine crash.

*Edit:

I’ve managed to work out that I was calling a physics enable/disable on a none physics root so this works but the item again will not move but will state it is socketed, if I were to disable physics in the actual actor it is fine, if it has physics enabled it won’t move to the socket position.

Ironically, if you start with the physics off, pickup the bag then drop it which will activate the physics and gravity after detaching it will fall correctly but after that it will refuse to pick back up and socket so the problem seems to lay with the issue that if the physics are activated at any time it will refuse to socket correctly.

I think they disallow attaching of physics objects for two major reasons:

  1. Attached objects create a transform hierarchy, but physics objects don’t move relative to anything - they are always relative to the world. Even if Unreal allowed this sort of attachment, the result would be ambiguous, or not necessarily what you want. Would the child move freely under forces applied to it, but also move with forces applied to the parent? Would the attachment work like a joint? What kind?

  2. If you assume that an attached child should just stick with the parent, and the engine were to let you do what you are asking, it would be functionally equivalent to what we have now anyway - physics in the parent object, and just collision on the child, since the child shouldn’t move from it’s relative socket transform.

Basically, I think they’re just trying not to let you do something that doesn’t make sense, or something that would waste cycles achieving the same result.

I was hoping that when a bag was dropped it would hit the floor, move around a bit, roll down the stairs etc so I wanted to do a simple disable on pickup, enable on put down.

I wonder if I can somehow override this or if it’s a frame issue, waiting on specific amount of frames for the physics to be fully disabled.
hmm

Basically what this topic talks about I’m currently doing but in C++ but it doesn’t wish to relocate the item, I wonder if there is a physics update call needed prior to attachment.

https://forums.unrealengine.com/showthread.php?61125-Need-help-picking-up-and-attaching-object-to-socket

Sorry if I was confusing - what you are doing should be entirely possible. You should be able to have the bag be a physics object when it’s not in the player’s hand, then disable physics before attaching it to the player. I don’t know why the engine is crashing when you try to toggle physics - I’ve never had this problem, I suspect it is something to do with your code, or you need an editor restart after making the change.

I did notice that some physics stuff doesn’t get entirely initialized until the next tick. If I make my character go ragdoll then try to immediately add a force to it, it just ignores the force. If I cache that force value and apply it the first frame after going ragdoll, the force is correctly applied. So it’s possible you are correct about waiting a frame after toggling physics, then updating your attachment.

After a ton of searching and asking I’ve come to no conclusive fix and no one seems to have any idea what is going on.

Has your game comes out? The character looks really cool.