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

Question Rigidbody added at runtime not working.

Discussion in 'Physics' started by MishDotCom, Feb 10, 2022.

  1. MishDotCom

    MishDotCom

    Joined:
    Jan 17, 2021
    Posts:
    34
    Hello! I am very far into developing my game and I realised that the character controller based controller was not working the way I would have liked it to work so I changed to a rigidbody based one. All good and well until I need to trigger a cutscene in game which triggers an animation clip in an animator. To allow the animation to play, I destroyed the rigidbody (because just making it kinematic didn't work because after the animation finished, it would return to it's position before the animation)

    Code (CSharp):
    1. //code for destroying rigidbody
    2. Destroy(transform.GetComponent<FixedJoint>());
    3. Destroy(rb);  
    and I would readd it to the gameobject using this code :

    Code (CSharp):
    1. rb = gameObject.AddComponent<Rigidbody>();
    2.         gameObject.AddComponent<FixedJoint>();
    3.         rb.constraints = RigidbodyConstraints.FreezeRotation;
    4.        
    The wierd behaiviour is that after adding the rigidbody, it appears as if it is kinematic, even though it isn't, it registers the velocity from the input code but does nothing. Basicly it has velocity but is not moving.

    Any help is welcome!

    Thank you!
     
  2. MishDotCom

    MishDotCom

    Joined:
    Jan 17, 2021
    Posts:
    34
    anyone?
     
  3. GamerXP

    GamerXP

    Joined:
    Mar 22, 2014
    Posts:
    74
    I think I had some smiliar problem with switching between Kinematic/Dynamic modes.
    If I remember correctly, I had to update RigidBody's position when switching mode (not Transform, but values in RigidBody itself). Can't say any more precisely without searching for the code.