2
3

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.

LineRenderer 上を動くオブジェクトを作る

Last updated at Posted at 2021-05-24

image.png
動画

単にLineの頂点を移動するだけでは移動速度が頂点位置のばらつきに依存してしまうので、Line上を等速で滑らかに移動できるようにします。
LineRenderer上を動かしたいオブジェクトに下記のスクリプトをアタッチすると、オブジェクトがライン上を移動します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveOnLine : MonoBehaviour
{
    [SerializeField] LineRenderer m_line=null;
    [SerializeField,Tooltip("速度[m/sec]")] float m_speed = 0.1f;
    [SerializeField,Tooltip("ライン上を動くObject")] Transform m_targetTr = null;
    int m_linePtr;
    float[] m_costArr;
    float m_remain;

    // Start is called before the first frame update
    void Start()
    {
        if (m_targetTr == null)
        {
            m_targetTr = transform;
        }
        init();
    }

    // Update is called once per frame
    void Update()
    {
        float delta = m_speed * Time.deltaTime;
        while (delta > 0f)
        {
            if (m_remain > delta)
            {
                m_remain -= delta;
                delta = 0f;
                break;
            }
            else
            {
                delta -= m_remain;
                m_linePtr = (m_linePtr + 1) % m_line.positionCount;
                m_remain = m_costArr[m_linePtr];
            }
        }
        Vector3 basePos = m_line.GetPosition(m_linePtr);
        if (m_remain > 0f)
        {
            float rate = 1f- m_remain / m_costArr[m_linePtr];
            basePos += (m_line.GetPosition((m_linePtr + 1) % m_line.positionCount) - basePos) * rate;
        }
        if (!m_line.useWorldSpace)
        {
            basePos = m_line.transform.position + Vector3.Scale(m_line.transform.rotation * basePos, m_line.transform.lossyScale);
        }
        m_targetTr.position = basePos;
    }

    private void init()
    {
        m_costArr = new float[m_line.positionCount];
        Vector3 pos = m_line.GetPosition(0);
        for (int i = 0; i < m_line.positionCount; ++i)
        {
            Vector3 nextPos = m_line.GetPosition((i + 1)% m_line.positionCount);
            m_costArr[i] = (nextPos - pos).magnitude;
            pos = nextPos;
        }
        if (!m_line.loop)
        {
            m_costArr[m_line.positionCount - 1] = 0f;
        }
        m_linePtr = 0;
        m_remain = m_costArr[0];
    }
}
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?