2014年12月28日日曜日

Unity シューティングメモ 敵の動き

敵のスクリプトです。

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
        // 移動スピード
        public float speed;
        // 弾を撃つ間隔
        public float shotDelay;
        // 弾のPrefab
        public GameObject bullet;
        // 弾の作成
        public Enemy GameObject;

        public void Shot (Transform origin)
        {
                Instantiate (bullet, origin.position, origin.rotation);
        }

        // 機体の移動
        public void Move (Vector2 direction)
        {
                rigidbody2D.velocity = direction * speed;
        }

        private Vector3 m_pos;
        void Start ()
        {
        m_pos = transform.localPosition;  // 形状位置を保持
        }
   
        void Update () {
        transform.localPosition = m_pos;  // 形状位置を更新
        m_pos.x += 0.05f;
                       }
}

http://japan.unity3d.com/developer/document/tutorial/2d-shooting-game/game/04.html
こちらを参考にしてすすめてますが、
組み方ははっきり言って無視w


共通部分を取り出し、別のコンポーネント”などはぜんぜん無視。
C#がベースになってますね。

public class Enemy : MonoBehaviour
まず、これは クラス名と型を宣言。
というか半分ぐらいいらないw

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
        // 移動スピード
        public float speed;
        // 機体の移動
        public void Move (Vector2 direction)
        {
                rigidbody2D.velocity = direction * speed;
        }
}

コンポーネントの使い方はこちら。
http://ws.cis.sojo-u.ac.jp/~izumi/Unity_Documentation_jp/Documentation/Manual/Using%20Components.html


using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
        // 移動スピード
        public float speed;
        // 機体の移動
        public void Move (Vector2 direction)
        {
                rigidbody2D.velocity = direction * speed;
        }
                void Start ()
        {
                // ローカル座標のY軸のマイナス方向に移動する
                Move (transform.up * -1);
        }
}

まとめ。

rigidbody2D.velocity
とは、

 void Start () を Updataに変更したら動かない。

ぐちゃぐちゃになってきた。
単純に座標を移動をすると壁にあたったときなどバグを起こすらしい。

void FixedUpdate ()
の中でしか、駄目とのこと。なんでわかれてんだろ。













0 件のコメント:

コメントを投稿

Ga4のtest

これを押すとリンクするよ 参考にした記事はこちら