- Home /
HingeJoint2D Teleportation
I'm making a game where the character sways back and forth on a hinge joint, and when the player presses space, it releases the hinge joint, and fires you in the direction you were pointing when you pressed space. I have a circle collider on the player, and I have a statement that checks for when the player is upright after rolling for a while. What I'm trying to get it to do is when the player rolls into an upright position, I want it to reset the hinge joint at that location, right under the player's feet (at the groundCheck pos). What it ends up doing is teleporting the character very far along the x axis. Help? Thanks!
Code:
public HingeJoint2D hinge;
public float force = 150f;
private JointMotor2D motor;
private JointAngleLimits2D limits;
public Transform groundCheck;
public LayerMask whatIsGround;
private bool grounded = false;
private float groundradius = 0.2f;
private bool anchorDone = false;
public float speed;
void Start () {
//Motor changer
motor = hinge.motor;
limits = hinge.limits;
limits.min = -30;
limits.max = 30;
}
void FixedUpdate () {
//Ground checking
grounded = Physics2D.OverlapCircle(groundCheck.position, groundradius, whatIsGround);
//Wobbling
if(hinge.limitState == JointLimitState2D.UpperLimit) {
motor.motorSpeed = -force;
hinge.motor = motor;
} else if (hinge.limitState == JointLimitState2D.LowerLimit) {
motor.motorSpeed = force;
hinge.motor = motor;
}
//Jump
if (grounded && Input.GetKeyDown(KeyCode.Space)) {
hinge.enabled = false;
rigidbody2D.AddForce(new Vector2 (0, 500));
anchorDone = false;
grounded = false;
}
}
void Update () {
//Reset wobble
if (grounded && !anchorDone) {
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.identity, speed * Time.deltaTime);
if (transform.rotation == Quaternion.identity) {
hinge.limits = limits;
hinge.anchor = groundCheck.position;
hinge.connectedAnchor = groundCheck.position;
hinge.enabled = true;
anchorDone = true;
}
}
}
Picture of what is happening when you become upright:
Answer by collinator7 · Aug 02, 2014 at 05:08 AM
SOLVED I had the a random rigidbody2d attached... Derp.