unity freeze position code example

Example 1: freeze axis in script

public Rigidbody2D rb;

void Start()
{
	//Freeze Rotation on X Axis
	rb.constraints = RigidbodyConstraints2D.FreezeRotationX;
    //Freeze Rotation on Y Axis
    rb.constraints = RigidbodyConstraints2D.FreezeRotationY;
    //Freeze Rotation on Z Axis
    rb.constraints = RigidbodyConstraints2D.FreezeRotationZ;

	//Freeze Position on X Axis
	rb.constraints = RigidbodyConstraints2D.FreezePositionX;
    //Freeze Position on Y Axis
	rb.constraints = RigidbodyConstraints2D.FreezePositionY;
    //Freeze Position on Z Axis
	rb.constraints = RigidbodyConstraints2D.FreezePositionZ;
  
    //Freeze All Axis (Rotation & Position)
    rb.constraints = RigidbodyConstraints2D.FreezeAll;
    //UnFreeze All Axis (Rotation & Position)
    rb.constraints = RigidbodyConstraints2D.None;
}

Example 2: freeze position unity c#

rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;

Example 3: rigidbody freeze position through code

rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;

Example 4: unity freeze position in script

using UnityEngine;

public class FreezePosition : MonoBehaviour
{
    public GameObject TheThingToFreeze;
  	
    public void	Start()
    {
    	TheThingToFreeze.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;  
    }
      
}

Example 5: how to freeze an object's position unity

rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;