0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UnityでNavMeshを使わず、特定のGameObjectを巡回させるスクリプト

Posted at
Patrol.cs
using UnityEngine;
using System.Collections;

public class Patrol : MonoBehaviour
{

    [SerializeField, TooltipAttribute("経由地点の数を入力し,シーン上に配置した空のオブジェクトをアサインします")]
    public Transform[] wayPoints;

    public Transform target;
    public float speed;

    public int currentRoot;


    void Update()
    {

        //配列に入れたTransformを順に巡る.AIを使っていればスムーズに曲がるがこれは鋭角に曲がる

        float step = speed * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, wayPoints[currentRoot].position, step);


        Vector3 pos = wayPoints[currentRoot].position;

        float test = Vector3.Distance(target.position, pos);
        transform.LookAt(pos);
                    
        if(Vector3.Distance(transform.position, pos) < 0.5f)
        {
          currentRoot = (currentRoot < wayPoints.Length - 1) ? currentRoot + 1 : 0;
        }

    }
}



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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?