甘酒のアプリ開発

一人でも多くの人を幸せにするアプリを作っていけたらなぁと思います。

【Unity】Constraintsの回転をスクリプトから制御する方法 「FreezeRotation」

Constraintsの回転をスクリプトから制御する方法をまとめてみました。

 

移動を制御したい場合はこちらをご覧ください。(_ _)

 

 

回転の制御

 

FreezeRotationX

 

説明:FreezeRotationX をオンにする。

Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX;

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NewBehaviourScript : MonoBehaviour{
  6.  
  7.     void Start()
  8.     {
  9.         var rb = GetComponent<Rigidbody>();
  10.         rb.constraints = RigidbodyConstraints.FreezeRotationX;
  11.     }
  12. }

 

 -------------------------------------------------------------------------------------------------

FreezeRotationY

 

説明:FreezeRotationY をオンにする。

Rigidbody.constraints = RigidbodyConstraints.FreezeRotationY;

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NewBehaviourScript : MonoBehaviour {
  6.  
  7.     void Start()
  8.     {
  9.         var rb = GetComponent<Rigidbody>();
  10.         rb.constraints = RigidbodyConstraints.FreezeRotationY;
  11.     }
  12. }

 

 -------------------------------------------------------------------------------------------------

FreezeRotationZ

 

説明:FreezeRotationZ をオンにする。

Rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ;

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NewBehaviourScript : MonoBehaviour {
  6.  
  7.     void Start()
  8.     {
  9.         var rb = GetComponent<Rigidbody>();
  10.         rb.constraints = RigidbodyConstraints.FreezeRotationZ;
  11.     }
  12. }

 

 -------------------------------------------------------------------------------------------------

FreezeRotation

 

説明:FreezeRotationX FreezeRotationY FreezeRotationZをオンにする。

Rigidbody.constraints = RigidbodyConstraints.FreezeRotation;

  • サンプルコード
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NewBehaviourScript : MonoBehaviour {
  6.     void Start()
  7.     {
  8.         var rb = GetComponent<Rigidbody>();
  9.         rb.constraints = RigidbodyConstraints.FreezeRotation;
  10.     }
  11. }