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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] How Do I Restrict Movement Along The Y Axis?

Discussion in 'Physics' started by Chris-Ramer, Jan 31, 2016.

  1. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    Alright, so this may sound like a stupid question, but it's not as simple as one would think.

    I semi-recently upgraded to Unity 5.3 and am trying to use the new constraints components under the Rigidbody2D. The only option that works in code is the Freeze Rotation Z, which is accessed by this code:

    Code (JavaScript):
    1. GetComponent.<Rigidbody2D>().freezeRotation = true;
    However, it is not possible to set the boolean of either of the freeze positions by using this same formula. I have tried this code to set the Freeze Position Y to true:

    Code (JavaScript):
    1. GetComponent.<Rigidbody2D>().freezePositionY = true;
    Any help would be greatly appreciated!

    EDIT: Thought it would be good to include the message I am receiving in the console:

    MissingFieldException: UnityEngie.Rigidbody2D.freezePositionY
     
    Last edited: Jan 31, 2016
  2. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    UPDATE: Through more trial and error, I used this code:

    Code (JavaScript):
    1. GetComponent.<Rigidbody2D>().constraints.FreezePositionY = true;
    And now I get this error message in the console:

    BCE0117: 'UnityEngine.RigidbodyConstraints2D.FreezePositionY' is read only.

    The problem I have with that message is that it's NOT read only. If it was, then why would it be in the inspector?

    Now I'm even more confused... o_O

    BUT, this means that I am now using the correct reference, right? I just can't modify it?
     
  3. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    UPDATE II: I decided to test to see if I can at least read the value of the Freeze Rotation Y boolean and I get this in return:

    FreezePositionY

    That's the name of the variable, not the value. I need the value!

    PS: From now on, I will not update mid-project. Since upgrading from 4.6, I've run into problems with the new SceneManager.LoadScene overriding the old Application.LoadLevel and now this... :p
     
  4. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    UPDATE III: Looking through the release patches for 5.3.1 and 5.3.2, I see some fixes that help me (particularly when it comes to unloading scenes/loading scene(s) additively). I will upgrade now for those fixes and for a potential fix to this problem. I am now thinking that the guys at Unity forgot to take this value off read only, so I hope they've fixed it since 5.3.0f4. I will report back with results.
     
  5. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    UPDATE IV: Unfortunately, the problem at hand still stands in 5.3.2f1.

    On a positive note, the fixes to unloading scenes will come in handy! :D
     
  6. Chris-Ramer

    Chris-Ramer

    Joined:
    Dec 9, 2015
    Posts:
    45
    SOLVED!

    I found this page that answered my question. Here is the correct code:

    Code (JavaScript):
    1. GetComponent<Rigidbody2D>().constraints = GetComponent.<RigidbodyConstraints2D>().FreezePositionY;
    What I found out is that you need to assign the value to the constraints (you can't just call the specific constraint).