LoginSignup
0
0

More than 5 years have passed since last update.

【Unity】~SIerからゲーム業界へ~ 第4-2回 敵

Posted at

image.png

基本機能

・プレイヤーを追尾してくる
・一定距離内に入った場合のみ追尾とする
・プレイヤーの攻撃を受けたら消滅

学んだ点

プレイヤー追尾のためにNavMeshAgentを使用した

Ememy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Enemy : BaseBehaviour {

    public GameObject target;
    NavMeshAgent agent;
    private Vector3 pPos;
    private Vector3 ePos;

    // Use this for initialization
    void Start () {
        agent = GetComponent<NavMeshAgent>();
        agent.speed = 0;
    }

    // Update is called once per frame
    void Update () {

        // プレイヤーとの距離を算出
        pPos = target.transform.position;
        ePos = transform.position;
        var dist = Vector3.Distance(pPos, ePos);

        // 一定距離に近づいたら動き出す
        if (dist < 10)
        {
            agent.speed = 2;
        }

        // 敵オブジェクトの目的地を指定
        agent.destination = pPos;

    }

    private void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Fire")
        {
            Destroy(this.gameObject);
        }
    }
}

0
0
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
0
0