Cannot get rigidbody constraint to unfreeze
public class Items : MonoBehaviour
{
Rigidbody rb;
public bool sweatBandsItem = false;
// Use this for initialization
void Start ()
{
rb.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
if (sweatBandsItem == true)
{
gameObject.FindWithTag("PushZone") = RigidbodyConstraints.None;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("SweatBands"))
{
other.gameObject.SetActive (false);
sweatBandsItem = true;
}
}
}
I'm having issues with unfreezing the position of a rigidbody. What I'm specifically trying to do is have my character able to push a cube after picking up an item (which I have working) and then unfreezing the rigidbody positions of all cubes with the tag "PushZone". I've tried several approaches and the one I've posted above was the last attempt. The vast majority of google searches have led me to outdated methods, and the few that weren't outdated simply didn't work for me. Any advice would be appreciated.
Answer by Subhanshu · Jul 14, 2017 at 11:05 AM
Hi @tcrouch
Here we will need two private variables.
private GameObject GO;
private Rigidbody RB2;
{
void Start()
{
//First we will find all the gameObjects whose tag is 'PushZone'.
GO=GameObject.FindGameObjectWithTag("PushZone");
//Then we will get the Rigidbody Component that is attached to the gameObjects
which are having 'PushZone' tag.
RB2=GO.GetComponent<Rigidbody>();
}
void Update()
{
// Whenever you want to unfreeze..
if(sweatBandsItem == true)
{
RB2.constraints=RigidbodyConstraints.None;
}
}
}
Whatever code is written in the Start method can be put in any method as per your desire. I mean, if you want to Unfreeze only after picking up an item, then simply paste that code (Start Method Code) right after the "picking up code".
Hope it will work.!
All the best for your further development process. (Y) :-)
Your answer
Follow this Question
Related Questions
How do I unfreeze z rotation in a script and then freeze it again 1 Answer
Why does my rigidbody move it's transform while rotating? 1 Answer
Rigidbody local position constraints 2 Answers
Cloth constraints don't constrain *o* 1 Answer
Rigidbody changes position when I add torque. How do I stop this? 1 Answer