- Home /
Hinge join 2d Swing by adding force to the connected rigidbody
Hello people,
I have a simple game mechanic in which I use hinge joint 2d to simulate the player swinging on a rope which is attached to a Kinematic rigid body. The kinematic rigid body has the HingeJoint2D component and my player is the connected rigidbody.
This setup works fine, but when I add a script that controls the swinging by adding force to the rigidbody of the player, then the character doesn't swing anymore and it gets stuck.
I am guessing that the fact that I'm accessing the connected rigidbody dynamically in the code is messing with the physics, am I right? if so, is there a way to overcome this issue and control the swinging using arrow keys?
The code to control the swinging (belongs to the player) is as follows
void Update () {
swing = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
else
{
jump = false;
}
}
void FixedUpdate()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(swing * swingSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (jump)
{
anim.SetBool("jump", true);
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpForce;
GetComponentInChildren<GrapplingHookController>().RetractHook();
}
}
Your answer
Follow this Question
Related Questions
how to make "addForce" only affect one of several connected objects (with hinge points) 0 Answers
Rigidbody2D linearDrag - Suddenly Change 0 Answers
add force on topdown bullet 1 Answer
Rigidbody2d.AddForce behaving odd 1 Answer
AddForce knockback x axis according to mouse.position more then y axis (2D game) 0 Answers