はじめに
ドラクエみたいなバトルシーンを作ろうと記事を探したのですが、見つからなかったので試行錯誤で作ってみました。おそらく効率の悪いコードかもしれないですが、記事書いてみます。
完成すると
はじめはPlayer, Enemyの配置
上がEnemy、下がPlayerとなるように配置。ここから作業!
・BoxCollider2Dを付ける。
・Rigidbodyを付ける。Rigidbodyの中のGravityScaleは0に。
・Playerには「Player」、Enemyには「Enemy」とタグづけ。
ここからスクリプトを張り付けていきます!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
private float speed = 0.05f;
public int PlayerHp;
public int PlayerMp;
//playerの動き制御(※今後重要になります)
public bool playermove;
// Start is called before the first frame update
void Start()
{
//playermoveをtrue(今だけ)
playermove = true;
}
// Update is called once per frame
void Update()
{
//playerの動きを制御trueだったら動ける
if (playermove == true)
{
Vector2 position = transform.position;
if (Input.GetKey("up"))
{
position.y += speed;
}
transform.position = position;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
//Enemyの初期設定
public int EnemyHp;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
コード解説
//Enemyの初期設定
public int EnemyHp;
//Enemyの初期設定
public int EnemyHp;
このように記述することで、unity側で敵によって敵のHPを変更することが出来ます!
ここから作業!
・それぞれにスクリプトを張り付ける
・Enemyを選択。Inspectorビュー上で、EnemyHpを設定。今回は1000に。
当たり判定の実装
今回はEnemyにPlayerが当たったらまでを実装します。
コードは以下の通りです。今回のScriptはBattleManagerにします。このスクリプトはPlayerに張り付けてください。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BattleManager : MonoBehaviour
{
//スクリプトの取得
PlayerScript player;
EnemyScript enemy;
//Playerの情報
public int PlayerHp;
public int PlayerMp;
public bool PlayerMove;
//敵の情報
private int EnemyHp;
private GameObject Enemy;
// Start is called before the first frame update
void Start()
{
PlayerMove = true;
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Enemy")
{
//当たったら表示
Debug.Log("当たったよ");
//playerの情報取得
player = this.gameObject.GetComponent<PlayerScript>();
PlayerHp = player.PlayerHp;
PlayerMp = player.PlayerMp;
//当たったenemyの情報取得
enemy = col.gameObject.GetComponent<EnemyScript>();
EnemyHp = enemy.EnemyHp;
Enemy = col.gameObject;
//playerの動きを止める
PlayerMove = false;
}
}
}
コード解説
//playerの情報取得
player = this.gameObject.GetComponent<PlayerScript>();
//playerの情報取得
player = this.gameObject.GetComponent<PlayerScript>();
このように書くことでplayerについてるPlayerScriptを取得することが出来ます。
Playerスクリプトを変更
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
private float speed = 0.05f;
public int PlayerHp;
public int PlayerMp;
//追加:BattleManagerスクリプトの取得
BattleManager player;
//playerの動き制御(※今後重要になります)
public bool playermove;
// Start is called before the first frame update
void Start()
{
//playermoveをtrueをコメントアウト
//playermove = true;
}
// Update is called once per frame
void Update()
{
//playermoveの取得
player = this.gameObject.GetComponent<BattleManager>();
playermove = player.PlayerMove;
//playerの動きを制御trueだったら動ける
if (playermove == true)
{
Vector2 position = transform.position;
if (Input.GetKey("up"))
{
position.y += speed;
}
transform.position = position;
}
}
}
コード解説
//playermoveの取得
player = this.gameObject.GetComponent<BattleManager>();
playermove = player.PlayerMove;
//playermoveの取得
player = this.gameObject.GetComponent<BattleManager>();
playermove = player.PlayerMove;
この部分では、BattleManagerのPlayerMoveの情報を取得しています。
再生
これでプレイヤーを動かせるようになり、敵に当たるとデバックで「当たったよ」とコンソールに表示されるようになります。
次回は、UIの設定を行います。