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
  4. Dismiss Notice

Rigidbody2d constraints not working

Discussion in '2D' started by weareallthereis, Oct 15, 2021.

  1. weareallthereis

    weareallthereis

    Joined:
    May 17, 2015
    Posts:
    8
    I am unable to freeze the constraints on my rigidbody2d with my object that uses hingejoint2d no matter what I do. I tried freezing it with code to no avail
    Code (CSharp):
    1.  chainsaw.transform.rotation = Quaternion.Euler(0, 0, angle);
    2.             GameObject.Find("bone_2").GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezePosition;
    3.            
    My arm detaches from its rotational point defined by the bone as shown in attached image
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Line 1 bypasses the physics system.

    Also, the physics system constraints don't retroactively repair stuff you did before they went into effect (eg, line 1 above, line 2 follows it)

    Steps to success:

    - Set the constraint(s) you want

    - ONLY call .MovePosition() or .MoveRotation() on the Rigidbody instance

    - NEVER manipulate the transform directly, otherwise you could expect it to glitch
     
  3. weareallthereis

    weareallthereis

    Joined:
    May 17, 2015
    Posts:
    8
    I get an error with rb.rotation * input of "operator * cant be applied to type float and Quaternion"
    Code (CSharp):
    1. Rigidbody2D rb = chainsaw.GetComponent<Rigidbody2D>();
    2. Quaternion input = Quaternion.Euler(0, 0, angle);      
    3.   rb.MoveRotation(rb.rotation * input);
    this is stupid, it was working fine before i switched my unity version and before i deleted a parent object of the chainsaw but I cant seem to recreate it
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    This has nothing to do with Unity or upgrading and as the error clearly says, it's just not valid to do "float * Quaternion". That has never worked and I'm not even sure what it would mean. You can only use that "*" operator with Quarternions on both sides.

    "rb.rotation" is a float. You're likely to have used "transform.rotation" before which is a Quaternion.

    Because this is 2D physics, it only knows about a Z rotation so make it easy on yourself and ignore creating Quats (3D rotations) and use a single float angle like this:
    Code (CSharp):
    1. var rb = chainsaw.GetComponent<Rigidbody2D>();
    2. rb.MoveRotation(rb.rotation + angle);
    (Rigidbody2D.MoveRotation)

    Also, please in the very least acknowledge people who are trying to help you rather than just move on to another problem. They are, after all, spending their time helping you.

    Thanks and good luck.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    I'm sorry you've had this issue. Please consider using proper industrial-grade source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    Here's how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally setting Unity up (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards