日々惑う -雑記ブログ-

プログラミングやUnityの使い方、ソシャゲ運営の手法など自身の考え方などをまとめていく。

4. 敵実装(ソースコードのみ)

ソースファイル

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {
    public int HP = 10;  // PlayerのHP

    // ダメージを受けたとき
    public void OnDamage(int damage)
    {
        HP -= damage;
    }
}


PlayerShot.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShot : MonoBehaviour {

    // 弾のスピード
    public float speed = 1.0f;

    // 弾が持っているRigidBody
    public Rigidbody rigid;
	
	// Update is called once per frame
	void Update () {

        // 物理で自分自身が進む力を、
        // 自分が向いている方向にspeed分だけ進むように強制書き換え
        rigid.velocity = transform.forward * speed;

        //// 自分の位置を、自分の位置 +
        //// 自分の向いている前方向ベクトル ✕ speedに変更する.
        //rigid.MovePosition(transform.position +
        //    transform.forward * speed);
    }

    // コライダーが当たったことを検知したとき
    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("コライダーがあたったよ!");
        //// 何かに当たったら自分を消す
        //GameObject.Destroy(gameObject);
        //// 何かに当たったら当たったやつを消す
        //GameObject.Destroy(collision.gameObject);
    }

    // トリガーが当たったことを検知する
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("トリガーがあたったよ!");
        // 何かに当たったら自分を消す
        GameObject.Destroy(gameObject);
    }
}

PlayerShooter.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShooter : MonoBehaviour {

    // 弾オブジェクトのプレハブ
    [SerializeField]
    GameObject shotPrefab;

    // 弾を生成するポイント.
    [SerializeField]
    GameObject shotPoint;

    // 弾を格納する空のゲームオブジェクト.
    [SerializeField]
    GameObject shotRoot;

    // 弾が生成される間隔(秒).
    [SerializeField]
    float shotInterval = 0.3f;

    // 前回弾を撃った時間.
    float prevShotTime = 0;
    
	// Update is called once per frame
	void Update () {
        // Zキーが押されたら、自分と同じ場所に弾オブジェクトを生成する.
        if (Input.GetKey(KeyCode.Z) && Time.time - prevShotTime > shotInterval)
        {
            // 弾が打たれたら前回弾が撃たれた時間を更新.
            prevShotTime = Time.time;

            GameObject newShot = Instantiate(shotPrefab);
            newShot.transform.position = shotPoint.transform.position;
            newShot.transform.rotation = shotPoint.transform.rotation;

            newShot.transform.parent = shotRoot.transform;
        }
    }
}

EnemyShot.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerShot : MonoBehaviour {

    // 弾のスピード
    public float speed = 1.0f;

    // 弾が持っているRigidBody
    public Rigidbody rigid;
	
	// Update is called once per frame
	void Update () {

        // 物理で自分自身が進む力を、
        // 自分が向いている方向にspeed分だけ進むように強制書き換え
        rigid.velocity = transform.forward * speed;

        //// 自分の位置を、自分の位置 +
        //// 自分の向いている前方向ベクトル ✕ speedに変更する.
        //rigid.MovePosition(transform.position +
        //    transform.forward * speed);
    }

    // コライダーが当たったことを検知したとき
    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("コライダーがあたったよ!");
        //// 何かに当たったら自分を消す
        //GameObject.Destroy(gameObject);
        //// 何かに当たったら当たったやつを消す
        //GameObject.Destroy(collision.gameObject);
    }

    // トリガーが当たったことを検知する
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("トリガーがあたったよ!");
        // 何かに当たったら自分を消す
        GameObject.Destroy(gameObject);
    }
}

EnemyShooter.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyShooter : MonoBehaviour {

    // 弾オブジェクトのプレハブ
    [SerializeField]
    GameObject shotPrefab;

    // 弾を生成するポイント.
    [SerializeField]
    GameObject shotPoint;

    // 弾を格納する空のゲームオブジェクト.
    [SerializeField]
    GameObject shotRoot;

    // 弾が生成される間隔(秒).
    [SerializeField]
    float shotInterval = 0.3f;

    // 前回弾を撃った時間.
    float prevShotTime = 0;
    
	// Update is called once per frame
	void Update () {
        // Zキーが押されたら、自分と同じ場所に弾オブジェクトを生成する.
        if (Time.time - prevShotTime > shotInterval)
        {
            // 弾が打たれたら前回弾が撃たれた時間を更新.
            prevShotTime = Time.time;

            GameObject newShot = Instantiate(shotPrefab);
            newShot.transform.position = shotPoint.transform.position;
            newShot.transform.rotation = shotPoint.transform.rotation;

            newShot.transform.parent = shotRoot.transform;
        }
    }
}