LoginSignup
6
6

More than 5 years have passed since last update.

敵キャラクターを追加して UnityChanを追いかけるようにする

Posted at

はじめに

前回 からの続きです。

今回は、敵キャラクターを追加して、UnityChanを追いかけるようにしてみます。

敵キャラクターをAssetストアからImportする

Assetストアで適当に敵キャラっぽいのを探します。無料で使えて足があるやつがいいですね、なんとなく。

その結果、これに決めました。
https://www.assetstore.unity3d.com/en/#!/content/2558

demo1_unity_-_RoadRunner_-_Web_Player.png

なかなかにキモいですね。こんなのには本能的に捕まりたくないので良い感じです。

敵キャラの設定をする

基本設定

  • Prefab化する
  • UnityChanと同じように、「Capsule Collider」「Rigidbody」「Animator(付属のもの)」「Scripe(EnemyControl.cs)」を設定する。

EnemyControl.cs

敵キャラのControlをするに当たって、ほとんどUnityChanと同じような処理が必要です(はしごやBarの処理など)。
少し構成を整理したら以下のようになりました。

プレゼンテーション1_pptx.png

UnityChanはPlayerのKey入力で stateにKey Eventを送りますが、EnemyはPlayerの位置を見て stateにEventを送るという流れにすれば良さそうです。

とりあえずアホみたいに追いかけるだけなら、以下のように書くことができました。

EnemyControl.cs
public class EnemyControl : BaseCharacterControl
{
    // UnityChan
    public Transform target;

    // Use this for initialization
    void Start () {
        StartBase();
        moveSpeed = 0.04f;
    }

    void Update() {
        state.KeyLeftRight(Math.Sign(target.position.x - transform.position.x));
        state.KeyUpDown(Math.Sign(target.position.y - transform.position.y));

        animator.SetBool("isgrounded", !state.IsFalling());
        animator.SetFloat("speed", state.IsMovingLeftRight() ? 0.3f : 0f);

        state.Tick();
    }
}

Animatorのパラメータや定義されている動作が今のUnityChanのとは少し違います。簡単に合わせられそうなものだけ合わせておきました。

さいごに

こんな猛犬のように追いかけられたらすぐ死にそうですね。。。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6