- Home /
Object jumps right after the attached HingeJoint2D is Enabled
Unity Version: 2017.1.0f3
I'm not new nor an amateur, but this thing is killing me...
Problem; When the hingejoint2D attached to the white object is activated, the white object jumps right after. is this a Unity bug, mentioned here?
When the white object enters the black one’s trigger, it activates automatically the hingejoint2D. While the ball travels between the two, hingejoint2d is deactivated.
Black ones are the static objects, with rigidbody2D & a triggered 2dcollider attached.
Setup: Left(White), Right(Black) object..
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallControl : MonoBehaviour
{
HingeJoint2D hingeJoint2D;
public float speed;
public float Motospeed;
bool gameStarted = false;
public float anchorGap;
JointMotor2D jointMotor;
// Use this for initialization
void Start()
{
gameStarted = false;
hingeJoint2D = gameObject.AddComponent<HingeJoint2D>();
hingeJoint2D.enabled = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump"))
{
if (!gameStarted)
{
gameStarted = true;
GetComponent<Rigidbody2D>().velocity = transform.up * speed * Time.deltaTime;
}
else
{
hingeJoint2D.enabled = false;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag=="B1")
{
jointMotor.motorSpeed = Motospeed;
jointMotor.maxMotorTorque = 10;
hingeJoint2D.enabled = true;
hingeJoint2D.autoConfigureConnectedAnchor = false;
hingeJoint2D.anchor = new Vector2(other.GetComponent<CircleCollider2D>().radius- anchorGap, other.GetComponent<CircleCollider2D>().radius- anchorGap);
hingeJoint2D.connectedBody = other.GetComponent<Rigidbody2D>();
hingeJoint2D.connectedAnchor = new Vector2(0, 0);
hingeJoint2D.useMotor = true;
hingeJoint2D.motor = jointMotor;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers
2D floating object 2 Answers
rigidbody2D.addforce in collider 1 Answer
AddForce with 2D Character 2 Answers
Rigidbody.velocity giving weird result 0 Answers