- Home /
How do I freeze a rigidbody2d using script?
I'm learning in class, I would like to know how we would go about freezing the position of my rigidbody2d on respawn. I've already asked my teacher, he personally has never done it, so he doesn't know how.
Answer by BlakeSchreurs · Oct 05, 2017 at 07:50 PM
There are a couple ways to do this. Try one or both to see which works best for your game.
If you change the RigidBody to be Kinematic, it will still retain physical properties, but not do any physics calculations for that body (Things can hit it, but it won't react, no reaction to gravity, etc).
var item = Instantiate<GameObject>(prefab);
var rb = item.GetComponent<Rigidbody2D>();
rb.bodyType = RigidbodyType2D.Kinematic;
Another option is to turn on all constraints.
var item = Instantiate<GameObject>(prefab);
var rb = item.GetComponent<Rigidbody2D>();
rb.constraints = RigidbodyConstraints2D.FreezeAll;
I tried that, I got two errors, here's my coding though, maybe it would help
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Level$$anonymous$$anager : $$anonymous$$onoBehaviour {
public float waitToRespawn;
public PlayerController thePlayer;
// Use this for initialization
void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
}
// Update is called once per frame
void Update () {
}
public void Respawm()
{
StartCoroutine ("RespawnCo");
}
public IEnumerator RespawnCo()
{
//Sets Player = False
thePlayer.gameObject.SetActive (false);
//$$anonymous$$oves player to Spawn Position, waits, then sets true
yield return new WaitForSeconds(waitToRespawn) ;
thePlayer.transform.position = thePlayer.respawnPosition;
thePlayer.gameObject.SetActive (true);
}
}
I refactored your RespawnCo a little, and added the RigidBody code. If this still creates errors, you'll need to show us what those errors are.
public IEnumerator RespawnCo()
{
// Turn off the RigidBody
var rb = thePlayer.gameObject.GetComponent<Rigidbody2D>();
if (rb != null) rb.bodyType = RigidbodyType2D.$$anonymous$$inematic;
else Debug.Log("No RigidBody?");
//Sets Player = False
thePlayer.gameObject.SetActive (false);
//$$anonymous$$oves player to Spawn Position, waits, then sets true
yield return new WaitForSeconds(waitToRespawn);
thePlayer.gameObject.SetActive (true);
thePlayer.transform.position = thePlayer.respawnPosition;
if (rb != null) rb.bodyType = RigidbodyType2D.Dynamic;
}
Gah, sorry, I didn't see that thePlayer was of type "PlayerController ", made a $$anonymous$$or adjustment to code above.
Answer by RandomIntroStudent · Oct 06, 2017 at 12:44 PM
I got the same error twice, it may be my fault, I'm just learning this, but, I got that "Type 'PlayerController' does not contain a definition for 'SetActive' and no extension method 'SetActive' of type "PlayerController' Could be found. Are you missing an assembly reference?"
Your answer
![](https://koobas.hobune.stream/wayback/20220612140342im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody rotation: Use hinge joint motor or MoveRotation script? 0 Answers
Tag detection not working with rigidbody 0 Answers
Can we refer to two rigidbodies2d in a single script? 0 Answers
Objects moving despite everything being removed 0 Answers
I can't use Rigidbody2D in my scripts, I can't find rigidbody2d in my script 3 Answers