Unreal Engine 4 C + + code dynamically created constraint

Source: Internet
Author: User

In the latest version of Unreal Engine 4.4, the Physicsconstraint in Blueprint is Bug-Blueprint cannot edit constraint's two actor components, and the only way to do this is through C + + code. There are many such questions, fortunately, the source code to you, and how to change with their own.

I want to implement the role swing function in the Thirdperson template, just like the Persian monkeys.



First, add a uphysicsconstraintcomponent to character blueprint:

Uproperty (visibledefaultsonly, Category = "Nanconstraint")
Tsubobjectptr<class uphysicsconstraintcomponent> Constraintcomp;

Also need a fixed physical object when tying a string of pegs, so add another uspherecomponent:

Uproperty (visibledefaultsonly, Category = "Nanconstraint")
Tsubobjectptr<class uspherecomponent> Blocksphere;

Initialize them in the character constructor:

Create constraint Component
Constraintcomp = Pcip. Createdefaultsubobject<uphysicsconstraintcomponent> (This, TEXT ("Constraintcomp"));
Fconstraintinstance Constraintinst;
Constraintcomp->constraintinstance = Constraintinst;

Blocksphere = Pcip. Createdefaultsubobject<uspherecomponent> (This, TEXT ("blockcomponent"));

Define two methods Swing () and unswing (), one start constraint, one disconnect constraint


void Ananprojectcharacter::swing ()
{
Fvector charlocation = Getactorlocation ();

Fvector spherelocation = charlocation + fvector (0, 0, Constraintlen);

Capsulerot = Capsulecomponent->getcomponentrotation ();


Blocksphere->setworldlocation (spherelocation);
Capsulecomponent->setsimulatephysics (TRUE);
Constraintcomp->setworldlocation (spherelocation);
Constraintcomp->setconstrainedcomponents (Blocksphere, Name_none, Capsulecomponent, NAME_None);
}

void Ananprojectcharacter::unswing ()
{
Capsulecomponent->setsimulatephysics (FALSE);
Constraintcomp->breakconstraint ();
Capsulecomponent->setworldrotation (Capsulerot);

}

It is important to note that character's capsulecomponent itself is not enabled for physical simulations, and in order for him to be put up, he needs to be added to the physical simulation after enabling constraint. Of course, when you disconnect, don't forget to disable it again. Capsulerot save before enabling physical simulation, capsulecomponent rotation, if not fixed, the role of constraint after the disconnection is likely to be head-down, very happy feeling.


Unreal Engine 4 C + + code dynamically created constraint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.