おっさんプログラマのUnity奮闘記

元WEB系プログラマのUnity奮闘記

おっさんのUnity入門 2Dオブジェクトで敵接触時のダメージアクションを加える

f:id:gomunpass:20180311144119j:plain

敵と接触したときに後方へ弾き飛ばされるアクションを実装してみます。

プレイヤーの移動スクリプトを修正します。

【PlayerMoveScript.cs】

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


public class PlayerMoveScript: MonoBehaviour {

	public Rigidbody2D rb2d;
	private bool isGrounded;
	private bool enemyTouch;
	GameObject Enemy;

	// Canvasの入れ物
	Canvas canvas;
	// UIControllerの入れ物
	UIController uicontroller;

	void Start () {
		// オブジェクトのRigidbody2Dを取得
		rb2d = GetComponent<Rigidbody2D> ();

		// UIControllerを取得する
		// canvasを取得してからそのコンポーネントであるスクリプトを
		// 取得する
		canvas = GameObject.Find ("CanvasWrapper")
			.GetComponentInChildren<Canvas>();
		uicontroller = canvas.GetComponent<UIController> ();

		// ENEMYとの接触状態を非接触であるfalseへ設定する
		// UIControllder内にあるEnemyTouch関数から接触状態を取得して代入する
		enemyTouch = uicontroller.EnemyTouch;

	}

	void Update () {
		// 地面に接触しているときだけ操作可能にする
		if (isGrounded == true) {		
			// 左右のキー入力を取得
			float moveparam = Input.GetAxis ("Horizontal");
			// 左右方向移動のためオブジェクトに力を加える
			rb2d.AddForce (Vector2.right * moveparam * 10f);

			// スペースキー入力でオブジェクトをジャンプさせる
			if (Input.GetKeyDown (KeyCode.Space)) {
				rb2d.AddForce (new Vector2 (0, 9.8f), ForceMode2D.Impulse);
				isGrounded = false;
			}
		}
	}

	void FixedUpdate () {

		// ENEMYとの接触状態を非接触であるfalseへ設定する
		// UIControllder内にあるEnemyTouch関数から接触状態を取得して代入する
		enemyTouch = uicontroller.EnemyTouch;

		Debug.Log (enemyTouch);

		if (enemyTouch == true) {
			BlowOff (Enemy);
			uicontroller.EnemyTouch = false;
		}
	}


	// 何らかのオブジェクト同士が接触したときの処理
	void OnCollisionEnter2D (Collision2D collision) {
		// 空中での連続ジャンプを抑制するため地面との接触を感知するフラグの操作
		// 地面とするオブジェクトにタグ(ground)をInspector上から追加しておく
		if (collision.gameObject.tag == "ground") {
			isGrounded = true;
		}

		// ENEMYに接触したときに動作する関数
		//(Inspectorで作成したenemyタグを事前にRED_ENEMYに適応しておく)
		// 接触したオブジェクトがenemyのtagが付いたオブジェクトだったら
		if (collision.gameObject.tag == "enemy") {
			// 接触した敵オブジェクトをEnemyに渡す
			Enemy = collision.gameObject;
			enemyTouch = true;
			EnemyTouchSend ();
		}
	}

	// PLAYERが他のオブジェクトと接触し続けているときに呼び出される関数
	void OnCollisionStay2D (Collision2D collision) {
		if (collision.gameObject.tag == "ground") {
			isGrounded = true;
		}
	}

	// PLAYERが他のオブジェクトと接触しなくなったときに呼び出される関数
	void OnCollisionExit2D (Collision2D collision) {
		if (collision.gameObject.tag == "ground") {
			isGrounded = false;
		}

		if (collision.gameObject.tag == "enemy") {
			enemyTouch = false;
			EnemyTouchSend ();
		}
	}

	// ENEMYとの接触時の動作を制御する関数
	void EnemyTouchSend () {
		// UIController.csのEnemyTouchを介してenemyTouchの値を書き換える
		uicontroller.EnemyTouch = enemyTouch;
	}


	// ENEMYと接触後に弾かれる動作を制御する関数
	void BlowOff (GameObject Enemy) {
		// 敵の位置を取得
		Vector3 enemyTFP = Enemy.transform.position;
		// プレイヤー自身の位置を取得
		Vector3 playerTFP = transform.position;

		if (enemyTFP.x - playerTFP.x > 0) {
			// 敵がプレイヤーより右側にいる時の処理
			playerTFP = enemyTFP + new Vector3 (-2, 1, 0);
			transform.position = playerTFP;
		} else if (enemyTFP.x - playerTFP.x <= 0) {
			// 敵がプレイヤーより左側にいる時の処理
			playerTFP = enemyTFP + new Vector3 (2, 1, 0);
			transform.position = playerTFP;
		}
	}

}


f:id:gomunpass:20180311144130j:plain

プレイヤーから見て後方へ弾き飛ばされるアクションを実装できました。