LoginSignup
1
5

More than 5 years have passed since last update.

Navigationで迷路からの脱出

Last updated at Posted at 2017-10-07

歩行可能なルートをつくる

NavMesh
https://docs.unity3d.com/jp/540/Manual/nav-BuildingNavMesh.html

壁と地面をStatic状態にする
Window > Navigation > Bake でベイクする

スクリーンショット 2017-10-07 6.22.11.png

キャラを動かす設定

NavMesh Agent
https://docs.unity3d.com/jp/540/Manual/nav-CreateNavMeshAgent.html

NavMeshAgentのプロパティ
https://docs.unity3d.com/jp/540/Manual/class-NavMeshAgent.html

目標地点の取得
navAgent.destination = targetPos
or
navAgent.setDestionation(targetPos)
https://docs.unity3d.com/jp/540/ScriptReference/NavMeshAgent.SetDestination.html

MyNavAgent.cs(キャラにアタッチする)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyNavAgent : MonoBehaviour {

    private UnityEngine.AI.NavMeshAgent navAgent;
    private Animator animator;
    private Vector3 lastPoint;
    private GameObject goal;

    void Start () {
        navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
        animator = GetComponent<Animator>();
        lastPoint = transform.position;
        goal = GameObject.Find("Goal");
    }

    void Update () {
        if(Input.GetMouseButtonDown(0)){
            // カメラからクリックした地点までのレイ
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray, out hit, 1000)){
                // クリックした地点に向かう
                navAgent.destination = hit.point;
                // ゴール地点を表示
                goal.GetComponent<Goal>().SetLoc(navAgent.destination);
            }
        }
        // スピードの測定
        float dist = Vector3.Distance(lastPoint, transform.position) * (1 / Time.deltaTime / navAgent.speed);
        lastPoint = transform.position;
        // AnimatorコンポーネントのControllerをダブルクリックしてParametersから確認
        animator.SetFloat("Forward", dist);
    }
}

ゴール地点に球を出現させる

Goal.cs(ゴールのオブジェクトにアタッチする)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Goal : MonoBehaviour {

    private UnityEngine.AI.NavMeshAgent navAgent;
    private Renderer myRenderer;
    private GameObject t;

    void Start () {
        t = GameObject.Find("ThirdPersonController");
        navAgent = t.GetComponent<UnityEngine.AI.NavMeshAgent>();
        myRenderer = GetComponent<Renderer>();
        myRenderer.enabled = false;
    }

    void Update () {
        if(myRenderer.enabled){
            if(Vector3.Distance(t.transform.position,transform.position) < 2){
                // 近づいたら見えないところに飛ばす
                transform.position = new Vector3(10000, 0, -10000);
                myRenderer.enabled = false;
            }
        }
    }

    public void SetLoc(Vector3 _p){
        // ゴール地点にセット
        transform.position = new Vector3(_p.x, 1, _p.z);
        myRenderer.enabled = true;
    }
}

動く障害物を置く

NavMesh Obstacle
https://docs.unity3d.com/jp/540/Manual/class-NavMeshObstacle.html

Carveにチェックを入れない

障害物がなくなるまで待つ

obstacle.gif

Carveにチェックを入れる

障害物を避けて別ルートを選択する
(計算負荷が高い)

curve.gif

1
5
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
1
5