Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

TargetJoint2D does not work anymore?

Discussion in '2D' started by beppim, Mar 22, 2016.

  1. beppim

    beppim

    Joined:
    Jul 2, 2015
    Posts:
    58
    I updated to 5.3.4f1 today because of the fix to the bug related to TargetJoint2D not activating colliders when instantiated by script.
    TargetJoint2D does not work anymore?
    I tried both via script both via editor.

    Indeed, dragging an object, I attach by script a TargetJoint2D component and, when the touch position is updated, I use this code. It worked in 5.3.3:

    touchedObject.GetComponent<TargetJoint2D>().target = myTouch.position;

    Now the object completely freezes and does not move until I release the finger.

    The same happens when an object is directly instantiated with a TargetJoint2D component from the editor: it looks like it does not try to reach the target, rather it starts oscillating as if it was trapped inside a cage.

    Does anyone know if something has changed in the TargetJoint2D configuration and usage, or is it to be considered a new bug?

    Thank you.
     
    Last edited: Mar 22, 2016
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,562
    I just tried it in 5.3.4f1 and it seems to works fine.

    I create a new scene, added a new GameObject with a Rigidbody2D and a TargetJoint2D and a new script that does the following:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SetTarget : MonoBehaviour
    4. {
    5.     void FixedUpdate ()
    6.     {
    7.         var worldPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    8.         GetComponent<TargetJoint2D> ().target = worldPos;
    9.     }
    10. }
    11.  
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,562
    Adding the TargetJoint2D dynamically also works exactly the same:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SetTarget : MonoBehaviour
    4. {
    5.     void OnEnable ()
    6.     {
    7.         if (GetComponent<TargetJoint2D> () == null)
    8.             gameObject.AddComponent<TargetJoint2D> ();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void FixedUpdate ()
    13.     {
    14.         var worldPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    15.         GetComponent<TargetJoint2D> ().target = worldPos;
    16.     }
    17. }
    18.  
     
  4. beppim

    beppim

    Joined:
    Jul 2, 2015
    Posts:
    58
    This is weird.... it started working after I moved the code from Update() to FixedUpdate().

    .... so sorry, and thanks: very precise and clean as usual.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,562
    You're welcome.

    It should still work however when used from 'Update'. It's just that nothing will move until the next 'FixedUpdate' and performing multiple moves in 'Update' is wasteful as only the last one before the next physics update will be used.