Quaternionクラスのサンプル

Quaternion.LookRotationのサンプル1


using UnityEngine;
using System.Collections;

public class test3 : MonoBehaviour
{
  public float maxRotSpeed = 40;
   public float miniTimeToReachTarget = 0.5f;
   Transform _tr, _camTr;
   float _velocity;

  void Start ()
   {
       _tr = transform;
       _camTr = Camera.main.transform;
   }
   
   void Update ()
   {
       Vector3 newRotation = Quaternion.LookRotation (_camTr.position - _tr.position).eulerAngles;
       Vector3 angle = _tr.rotation.eulerAngles;
       
       _tr.rotation = Quaternion.Euler (
           angle.x,
           Mathf.SmoothDampAngle (angle.y, newRotation.y, ref _velocity, miniTimeToReachTarget, maxRotSpeed),
           angle.z);
   }
}

Quaternion.FromToRotationのサンプル1

カメラの見ている方向にオブジェクトを向ける

using UnityEngine;
using System.Collections;

public class test4 : MonoBehaviour {
  
   Transform tr,cam;
   
   void Start () {
       tr = transform;
       cam = Camera.main.transform;
   }
   
   void Update () {
       
       Vector3 rot = Quaternion.FromToRotation(tr.forward,cam.forward).eulerAngles;
       rot.x=0;
       rot.z=0;
       
       tr.rotation = Quaternion.Euler(rot)*tr.rotation;
       
   }
}



Quaternion.AngleAxisのサンプル1


using UnityEngine;
using System.Collections;

public class AngleAxisTest : MonoBehaviour {
  
   public GameObject cube;
   float angle;
   Vector3 center,pos,r,direction;
   
   // Use this for initialization
   void Start () {
   }
   
   // Update is called once per frame
   void Update () {
       center = new Vector3(0.0f,0.0f,0.0f);    //円の中心位置
       angle += Time.deltaTime * 100 % 360;        //角度
       r = Vector3.right * 5;                    //半径の指定
       direction = Vector3.up;                    //軸の方向
       
       pos = Quaternion.AngleAxis(angle,direction) * r;
       cube.transform.position = pos + center;
   }
   
   void OnDrawGizmos(){
       float depth = 5.0f;
       Gizmos.DrawRay(center,direction*depth);    //軸の表示
   }
}


円錐の表面に沿ったベクトルを作成してオブジェクトを飛ばす


using UnityEngine;
using System.Collections;

public class QuaternionScript : MonoBehaviour {

  public GameObject blowoutObjectPrefab;
   
       float radius = 0.5f;    //円錐の半径 
       float PIangle = 0;        //-なら左側、+なら右側へ回り込んでいく 

      float forwardBackAngle = 0;
       float speed =10;
   
       Vector3 blowout;
   
   // Use this for initialization
   void Start () {

      Vector3 xz = Quaternion.AngleAxis(PIangle,Vector3.up)*(radius * Vector3.right);
       Vector3 dir = xz + Vector3.up;    //円錐表面のベクトルを採取
       dir.Normalize();
       
       Vector3 xz2 = Quaternion.AngleAxis(forwardBackAngle,Vector3.forward) * dir;
       //x軸を軸とした前後の回転(これはおまけでこういう事ができるよって意味の処理) 
       
       blowout = xz2*speed;
   }
   
   float time;
   
   // Update is called once per frame
   void Update () {
       time += Time.deltaTime;
       
       if(time > 0.1f){
           GameObject go = Instantiate(blowoutObjectPrefab,this.transform.position,Quaternion.identity) as GameObject;
           go.rigidbody.velocity = blowout;
           
           time=0;
       }
   }
}

配置したオブジェクトグループを回転移動させてみる


using UnityEngine;
using System.Collections;

public class AngleAxisTest : MonoBehaviour {
  
   public GameObject prefabPiece;
   int gridNum = 3;
   float gridScale = 1.5f;
   GameObject[] pieceGroup;
   GameObject root;
   Bounds prefabBounds;
   Vector3[] startPos;
   
   // Use this for initialization
   void Start () {
       
       prefabBounds = prefabPiece.GetComponent<MeshFilter>().mesh.bounds;
       
       Vector3 pitch = prefabBounds.size * gridScale;
       pitch.y = 0;
       Bounds gridZoneBounds = new Bounds(Vector3.zero,  pitch * gridNum);
       Vector3 gridZoneOffset = gridZoneBounds.max - pitch / 2;                //原点(0,0,0)からのオフセット
       
       int grid = gridNum * gridNum;
       //intに対する割算の性質を利用している int の 5%3 = 2 であり 5/3 = 1 になる 
       for (int i = 0; i < grid; i++) {
           int ix = i % gridNum; 
           int iz = i / gridNum;
           GameObject clone = Instantiate(prefabPiece,new Vector3(ix * pitch.x , pitch.y , iz * pitch.z), Quaternion.identity) as GameObject;
           clone.transform.position -= gridZoneOffset;
           clone.tag = "enemy";
       }
       
       pieceGroup = GameObject.FindGameObjectsWithTag("enemy");
       
       //親子関係の構築 親子の構築はTransformコンポーネントで行う
       root = new GameObject("root");
       root.transform.position = Vector3.zero;
       startPos = new Vector3[pieceGroup.Length];
       
       for (int i = 0; i < pieceGroup.Length; i++) {
           startPos[i] = pieceGroup[i].transform.position;
           pieceGroup[i].transform.parent = root.transform;
       }
       
   }
   
   float stepTime;
   float angle = 0;
   Vector3 rotDirection = Vector3.up;
   // Update is called once per frame
   void Update () {
       stepTime += Time.deltaTime;
#if true
      angle = stepTime *100 % 360;
       for (int i = 0; i < pieceGroup.Length; i++) {
           pieceGroup[i].transform.position = Quaternion.AngleAxis(angle,rotDirection) *  startPos[i];
       }
#else
      root.transform.Rotate(Vector3.up * Time.deltaTime * 100);
#endif

  }
   
   void OnDrawGizmos(){
       Gizmos.DrawRay(Vector3.zero , rotDirection * 5);
   }
}

メニュー



  • 最終更新:2014-06-13 12:12:18

このWIKIを編集するにはパスワード入力が必要です

認証パスワード