Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Adding FixedJoint2D with script with custom damping ratio?

Discussion in 'Getting Started' started by ethancremepie, Apr 22, 2024.

  1. ethancremepie

    ethancremepie

    Joined:
    Apr 19, 2018
    Posts:
    1
    I am trying to solve a simple thing, and maybe I am not searching for it right but any help appreciated. I am adding a FixedJoint2D to a rigid body, but I don't know how to add that FixedJoint2D with a damping ratio of 1. This is the code I'm using for that.

    Code (CSharp):
    1.  if (hold)
    2. {
    3.      gameObject.AddComponent(typeof(FixedJoint2D))
    4. }
    How do I get the joint I've added to have a damping ratio of 1? Or any of the variables associated with that fixed joint changed at all? Thanks for help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,375
    AddComponent<T> returns a reference.

    Assign that to a local variable and you can do what you like with it parameter-wise.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,375
    Code (csharp):
    1.                 var fj : FixedJoint2D;
    Wherever you got that from, never go back there. Why? The above code is Javascript / UnityScript which has been deprecated for 10+ years and extinguished from Unity for at least 7 years.

    The correct C# syntax would be:

    Code (csharp):
    1. var fj = gameObject.AddComponent<FixedJoint2D>();
    2. fj.whatever = set-to-whatever;
    See the docs for FixedJoint2D to learn what
    whatever
    can be.