LoginSignup
0
0

[TBU 更新予定] Unityでテキトードローンを動かす

Last updated at Posted at 2023-11-12

Unityでテキトードローンモデルを動かしたい

以下のようなことを目指します。完了次第記事を更新予定。

  1. Unityでキーボード操作によりドローンモデルを動かす
  2. UnityでNavMeshを使ってドローンモデルを動かす
  3. ROSから送信されてきたウェイポイントに自動でドローンモデルを動かす

使うモデルは以下。

1 やってみた

キーボードでドローンを動かすところまで完了。

DroneTest01_06.png

2 NavMesh使ってウェイポイント配列を巡回

PlaneにはNavMeshSurfaceをアタッチします。特に設定は不要なので「Bake」します。

エージェント(ドローンモデル)に以下のコードをアタッチします。また、Nav Mesh Agent もアタッチします。

コード


// Patrol.cs
using UnityEngine;
using UnityEngine.AI;
using System.Collections;


public class Patrol : MonoBehaviour
{

    private NavMeshAgent agent;
    private int destPoint = 0;
    private Vector3[] wp = new Vector3[999];


    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        agent.autoBraking = false;

        Transform tf = new GameObject().transform;
        wp[0] = new Vector3(-2, 0, 2);
        wp[1] = new Vector3(2, 0, 2);

        GotoNextPoint();
    }


    void GotoNextPoint()
    {

        agent.destination = wp[destPoint];

        if (destPoint == 0)
        {
            destPoint = 1;
        }
        else
        {
            destPoint = 0;
        }
    }


    void Update()
    {
        if (!agent.pathPending && agent.remainingDistance < 0.5f)
            GotoNextPoint();
    }
}

Drone_patrol.gif

参考

NavMeshの基本に関する参考
https://vanikki.com/unity-navmesh/

記事作成時間

記事作成時間1 : 2023/11/13 0時33分

記事作成時間_テキトードローン_01.png

NavMesh更新時の時間

記事作成時間_テキトードローン_02.png

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