- Home /
How to change settings to a rigidbody when adding it to a gamobject?
I am trying to make it so that when a particular gameobject touches a game object with this script attached to it, it will add a rigidboy to the object causing it to fall and disable the collider so it can pass through objects, and after a predesignated amount of time cause it to reappear when it originally was turn destroy it's rigidbody and turn on it's collider, I got it all to work perfectly except for one tiny detail, I need to freeze the rigidbody's rotation on the z axis, how to I do this?
HERE IS MY CODE:
public float respawnDelay; public float WaitTime;
public GameObject ground;
public Rigidbody2D rb;
public Collider2D goingThroughObjects;
private Vector3 whereToTeleport;
// Use this for initialization
void Start ()
{
goingThroughObjects = GetComponent<Collider2D>();
rb.isKinematic = true;
goingThroughObjects.enabled = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "HeadStomp")
{
StartCoroutine("TouchedBreakableObjectCo");
}
}
public IEnumerator TouchedBreakableObjectCo()
{
whereToTeleport = ground.transform.position;
yield return new WaitForSeconds(WaitTime);
ground.AddComponent<Rigidbody2D>();
rb = GetComponent<Rigidbody2D>();
yield return new WaitForSeconds(0.2f);
goingThroughObjects.enabled = false;
yield return new WaitForSeconds(respawnDelay);
Destroy (rb);
goingThroughObjects.enabled = true;
ground.transform.position = whereToTeleport;
PLEASE HELP
people I wanted to come help me:
@ UnityCoach
Answer by Hellium · Oct 09, 2017 at 03:37 PM
As indicated by tormentoarmagedoom, you can do the following :
// ...
rb = GetComponent<Rigidbody2D>();
rb.constraints = RigidbodyConstraints2D.FreezeRotation ;
// ...
Be careful, Rigidbody2D.constraints uses bitwise operations. For example, if you want to prevent the rotation on all the axes, and the translation on the X axis, use :
rb.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation ;
ok, I didn't know how to do that before but there is one problem with the code, I am working with a Rigidbody2D, not just a Rigidbody, and now visual studio is co$$anonymous$$g back saying that it cannot convert a normal Rigidbody, into a Rigidbody2D, is there a way to change it to work with a Rigidbody2D?
Answer by tormentoarmagedoom · Oct 09, 2017 at 07:58 AM
Hello !!
You have Rigidbody.freezeRotation and Rigidbody.constraints.
What do you mean by that? are you suggesting that I make a line of code that says: rb.freezeRotation.z; if so, please tell me, if not please tell me what you mean by that.
@tormentoarmagedoom oops sorry, forgot to add the at symbol and your name.
Your answer
Follow this Question
Related Questions
Box and circle Collider doesn't work 1 Answer
Disable Collider After two Second of Being Triggered and enable it again 3 Answers
How do I disable all rigidbodies and colliders in a scene in unity? 2 Answers
Why does Unity disable the Collider when I Destroy the Rigidbody? (2D) 1 Answer
Move a Rigidbody2d After Collision 0 Answers