Question by
alexandrumarcel · Feb 25, 2020 at 11:11 PM ·
2dragdollhingejoint2d
Change 2D Player position based on Enemy Position. Hinge Joints and Angle Limits.
Hi there!
I have a stickman ragdoll that's hold together by a couple of Hinge Joints with angular limits. There's also an enemy who can change his position so it can be on the left side of the player or the right side of him.
Both stickman's layout is this:
Empty GameObject:
-Head
-Torso
-ArmR
-ArmL
-LegR
-LegL
The ArmR
and ArmL
has an angular limit of 0
min, 90
max.
What's the easier way to inverse stickman's angular limit or rotate the stickman to look at the enemy?
This is with what I've come but it doesn't work as intended:
public void ChangePos(float pos)
{
HingeJoint2D hinge;
JointAngleLimits2D limits;
JointMotor2D motor;
if (pos > 0 && GetPlayerCanMove()) // -> right
{
Debug.Log("Changed Pos!");
hinge = head.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = 20;
hinge.limits = limits;
hinge = legUpperRight.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = 90;
hinge.limits = limits;
hinge = legUpperLeft.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = 90;
hinge.limits = limits;
hinge = legRight.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = limits.max * 30;
hinge.limits = limits;
hinge = legLeft.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = 30;
hinge.limits = limits;
}
else if (pos < 0 && GetPlayerCanMove() && weaponLeft.activeSelf)
{
hinge = head.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = -20;
hinge.limits = limits;
hinge = legUpperRight.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = -90;
hinge.limits = limits;
hinge = legUpperLeft.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = -90;
hinge.limits = limits;
hinge = legRight.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = -30;
hinge.limits = limits;
hinge = legLeft.GetComponent<HingeJoint2D>();
limits = hinge.limits;
limits.max = -30;
hinge.limits = limits;
}
}
Is there an easier way to do this?
Comment
Your answer
Follow this Question
Related Questions
How can I make HingeJoint2D more flexible? 0 Answers
Ragdoll 2D hinge joint limit issue 0 Answers
2D active ragdoll holding object 1 Answer
Can't see Hinge Joint 2d anchor points 2 Answers