65. ゲームオブジェクトを回転させるスクリプト
using UnityEngine;
using System.Collections;
public class CubeScript : MonoBehaviour {
" // Use this for initialization
" void Start () {
" }
"
" // Update is called once per frame
" void Update () {
" " transform.Rotate(Vector3.up * 90 * Time.deltaTime);
" }
}
12年9月7日金曜日
68. スクリプト解説
使用するネームスペースの指定
using UnityEngine;
using System.Collections;
public class CubeScript : MonoBehaviour {
}
全てのC#スクリプトはMonoBehaviourを継承する
C#では継承は “:” で表す
クラスの宣言
12年9月7日金曜日
69. スクリプト解説
// Use this for initialization
void Start () {
//初期化処理を記述するメソッド
}
// Update is called once per frame
void Update () {
//毎フレームごとの処理を記述するメソッド
" transform.Rotate(Vector3.up * 90 * Time.deltaTime);
}
このスクリプトがアタッチされた
ゲームオブジェクトを回転
(あとで詳しく解説)
12年9月7日金曜日
73. スクリプトを変更
using UnityEngine;
using System.Collections;
public class CubeScript : MonoBehaviour {
public float rotSpeed = 90.0f; //回転速度
" // Use this for initialization 回転速度をフィールドに
" void Start () {
" }
"
" // Update is called once per frame
" void Update () {
" " transform.Rotate(Vector3.up * rotSpeed * Time.deltaTime);
" }
}
12年9月7日金曜日
74. インスペクタを確認
✤ Cubeを選択してインスペクタを見ると
今作成した変数がGUIに表示されている!
h !
yl!
プログラム中でpublic で宣言した変数は
t
S
インスペクタで編集できる
プログラムを書き換えずに、プレイしながら最適な値に調整できる
12年9月7日金曜日
79. インスタンス生成
✤ スクリプトを用いてプレハブからインスタンスを生成する
✤ Cubeをプレハブにしてください
✤ CubeMakerスクリプトを作成し、Sphereにアタッチしてください
✤ CubeMakerクラス(の内部)を次のようにしてください
public class CubeMaker : MonoBehaviour {
public GameObject gObject;
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("a")){
GameObject.Instantiate(gObject, transform.position, transform.rotation);
}
}
}
✤ インスペクタで、G ObjectにCubeのプレハブをドロップしてください
12年9月7日金曜日
80. スクリプト解説
public class CubeMaker : MonoBehaviour {
public GameObject gObject;
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("a")){
GameObject.Instantiate(gObject, transform.position, transform.rotation);
}
}
}
GameObjectクラス:全てのゲームオブジェクトはGameObject型
Input.GetKeyDown:キーが押された時にtrue
GameObject.Instantiate:指定したオブジェクトの複製を作成するメソッド
transform.position:オブジェクトの位置を保持
transform.rotation:オブジェクトの回転を保持
12年9月7日金曜日