転職を繰り返したサラリーマンの多趣味ブログ

30才未経験でSEに転職した人の多趣味ブログ

Unity

【Unity】Imageの表示順を変更する

// 煙のimageを取得 GameObject smoke = (GameObject)Instantiate(smokePrefab); // canvasGameオブジェクトの子供に設定 smoke.transform.SetParent(canvasGame.transform, false); smoke.transform.SetSiblingIndex(2);

【Unity】imageの大きさを変更する方法

float scale = 1.0f; transform.localScale = new Vector3(scale, scale, 1.0f);

【Unity】レベルによって、表示するイメージを変える方法

public Sprite[] templePicture = new Sprite[3]; public void SetTemplePicture(int level) { GetComponent<Image>().sprite = templePicture[level]; }</image>

【Unity】経過時間をチェックする方法

// 時間の差分をチェック // DateTime型同士の計算は、TimeSpan型になる TimeSpan timeSpan = DateTime.UtcNow - lastDateTime; // RESPAWN_TIME定数の秒数をTimeSpan型に変換 if(timeSpan >= TimeSpan.FromSeconds(RESPAWN_TIME)) { while(timeSpan >= Time…

【Unity】現在時刻を取得する

DateTime lastDateTime = DateTime.UtcNow;

【Unity】Findメソッドの使い方

private GameObject gameManager = GameObject.Find("GameManager"); gameManager.GetComponent<GameManager>().GetKey();</gamemanager>

【Unity】タップを検出する方法

1.EventTriggerコンポーネントを追加する。2.Add new Event Type / PointerEnterを選択する。3.+をクリックし、タップした際に呼び出したいメソッドがあるゲームオブジェクトをセットする。4.No Functionをクリックし、対象のメソッドを選択する。

【Unity】Prefabからゲームオブジェクトを自動生成する方法②

// Prefabから新しいインスタンスを作成 GameObject orb = (GameObject)Instantiate(orbPrefab); // CanvasGameオブジェクトを親オブジェクトに設定 orb.transform.SetParent(canvasGame.transform, false); // 生成したインスタンスの位置を設定 orb.transf…

【Unity】UIの表示順を調整する

Cancas / Order in Layerの数値を大きくする。 そうすると、数値が大きいものほど前面に表示される。

【Unity】イメージをタッチ判定の対象外にする方法

Image / Raycast Targetをオフにする。

【Unity】背景の配置

1.UI / Imageをクリック。2.Source Imageのアイコンをクリックし、表示したいスプライトを選択。3.Set Native Sizeをクリックすると、もとの画像の大きさで表示される。

【Unity】ゲーム製作日記 iOSアプリ第3弾を作り始める

iOSアプリをAppStoreへ公開するまでの流れは一通り分かったので、次は色々こだわったゲームを作ろうと思う。けど、まだまだ自分ができることは少ないので、あくまで出来る範囲で。 まずは今作っているゲームの概要から。基本は「落ちゲー」で、ひたすら落下…

【Unity】ゲームオブジェクトを自動で動かし、オブジェクトに当たると反射する方法2D版

// 移動速度 public float speed = 5.0f; // Rigidbodyコンポーネント Rigidbody2D rb; // Use this for initialization void Start() { // Rigidbodyコンポーネント取得 rb = GetComponent<Rigidbody2D>(); //右上にボールを動かす rb.AddForce((transform.up + transfor</rigidbody2d>…

【Unity】iPhoneアプリ第2弾を公開しました。プログラムを覚えたら、何かを作ろうというがそれが結構難しい。

コツコツとUnityを使ったアプリ製作を続けてます。 uuc1h.hatenablog.jp 自分は全くのド素人から30歳でSEに転職し、プログラムを覚えた。プログラムを覚えるために様々な入門書を読んだ。オススメな入門書は、やっぱりサンプルがついているタイプ。本の通り…

【Unity】一定間隔で弾を撃ち続けるスクリプト

// 弾を撃つ間隔 public float shotDelay = 3.0f; // Use this for initialization IEnumerator Start () { rb = GetComponent<Rigidbody2D>(); while (true) { shot(); yield return new WaitForSeconds(shotDelay); } } private void shot() { // プレハブ生成 Instanti</rigidbody2d>…

【Unity】キャラクターを自動で左右に移動させるスクリプト

// 移動速度 public float speed = 2.0f; // Rigidbody private Rigidbody2D rb; // Use this for initialization void Start () { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update () { // 移動 move(); } private void move() { r</rigidbody2d>…

【Unity】ジャンプ処理

// ジャンプの力 public float jumpPower = 40; // ジャンプ中判定 private bool isJump = false; // 接地判定 private bool isGround = false; // Update is called once per frame void FixedUpdate () { // Playerの移動速度、方向をセット setMoveDirect…

【Unity】UI上のボタンでオブジェクトを操作する方法

1.UI / Buttonオブジェクトを作成する。 2.作成したButtonオブジェクトに、Event Triggerコンポーネントをセットする。 3.スクリプトは下記の通り。 public class Controller : MonoBehaviour { // 移動速度 public float moveSpeed = 3.0f; // 変数用移動速…

【Unity】OnCollisionEnterの接触判定

ColliderコンポーネントのIsTriggerチェックは外しておく。 // コライダーとの接触判定 private void OnCollisionEnter(Collision other) { if(other.gameObject.tag == "Controller") { Destroy(gameObject); Destroy(other.gameObject); } }

【Unity】オーディオソースをソースコードで制御し、鳴らす方法

AudioSourceコンポーネントをアタッチし、AudioClipにアタッチしておく。 private AudioSource shotSound; void Start () { shotSound = GetComponent<AudioSource>(); } void shot() { shotSound.Play(); }</audiosource>

【Unity】BGMを鳴らす方法

Main CameraのAudio Sourceコンポーネントに、オーディオソースをアタッチする。・Loopにチェックを入れる。・Play On Awakeにチェックを入れる。

【Unity】GameObjectをn秒後に削除する方法

public float lifeTime = 3.0f; void Start () { Destroy(gameObject, lifeTime); }

【Unity】プレイヤーの移動範囲を画面内に制限する方法

void Clamp() { // 画面左下のワールド座標をビューポートから取得 Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); // 画面右上のワールド座標をビューポートから取得 Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1,…

【Unity】ゲームスタートからの秒数をUIに表示し、記録をつけるスクリプト

public class GameManager : MonoBehaviour { public Text time; public Text highTime; private int seconds; private void Start() { StartCoroutine(CountTime()); } // Update is called once per frame void Update () { time.text = "時間:" + second…

【ゲーム開発記】赤いボールを撃ち落とせ①

unityroomにあげる用のゲームを作り始めた。ちなみに前回unityroomに投稿したゲームは、閲覧数が100を超えてた。作ったゲームが実際に遊んでもらっていることが分かったので、非常にうれしい。 赤いボールからひたすら逃げろ | 無料ゲーム投稿サイト unityro…

【Unity】マウスをクリックしたら、弾を発射する方法

public GameObject bullet; // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)) { shot(); } } void shot() { Instantiate(bullet, transform.position, transform.rotation); } ちなみに、弾の方のスクリプトは以下。 p…

【Unity】GameObjectをキーボードで操作する方法②

public float speed = 10; Rigidbody rb; void Start () { rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update () { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); Vector2 direction = new Vec</rigidbody>…

【Unity】シーンの再読み込み方法

>|cs| // 現在のシーン番号を取得 int sceneIndex = SceneManager.GetActiveScene().buildIndex; // 現在のシーンを再読込する SceneManager.LoadScene(sceneIndex); ||

【Unity】ハイスコアの保存と表示

// ハイスコアを更新 if(PlayerPrefs.GetInt("HighScore") < allSeconds) { // ハイスコアの保存 PlayerPrefs.SetInt("HighScore", allSeconds); } // ハイスコアの呼び出し highScore.text = "HIGH SCORE:" + PlayerPrefs.GetInt("HighScore"); // 変更され…

【Unity】Prefabから自動生成する際、生成位置をランダムにする方法

Vector3 vector3; public void createEnemy() { vector3 = new Vector3(Random.Range(-5.0f, 5.0f), transform.position.y, transform.position.z); // Enemyを作成する GameObject enemy = (GameObject)Instantiate(enemyOrigin, vector3, Quaternion.ident…