LoginSignup
0
0

More than 3 years have passed since last update.

【Unity】事前に決めた座標を行ったり来たりする方法

Posted at

行ったり来たりする処理

身近で行ったり来たりする処理どうする?的な話題になったので自分なりに考えた処理

サンプルコード

    [SerializeField, Header("移動速度")]
    float speed = 5;

    [SerializeField, Header("感知できる距離")]
    float range = 0.5f;

    [SerializeField,Header("移動したい座標")]
    Vector3[] movePath = new Vector3[3];
    Vector3 direction;

    int pathValue = 0;                  //現在どのパスなのか
    int nextPathValue;                  //次の移動したいパス

    enum MoveType
    {
        MoveOn,
        Return
    }

    MoveType moveType;

    private void Start()
    {
        //初期位置を記憶しておく(配列の0番目)
        movePath[0] = transform.position;

        //次の座標のパス
        nextPathValue = 1;
        moveType = MoveType.MoveOn;
    }

    private void Update()
    {
        Move();
    }

    /// <summary>
    /// 動くよ
    /// </summary>
    void Move()
    {
        //移動する向きベクトルを作成
        direction = movePath[nextPathValue] - movePath[pathValue];
        //方向ベクトルを作成
        direction.Normalize();

        Debug.Log(pathValue +"  "+nextPathValue);

        //移動
        transform.position += direction * Time.deltaTime * speed;

        //目標地点に近づいたか評価
        if (CheckDistance(transform.position, movePath[nextPathValue], range))
        {
            //次のパスに移動先を変更
            NextDesignatedLocation();
        }
    }
    /// <summary>
    /// 範囲を指定してその範囲ないか調べる関数
    /// </summary>
    /// <param name="_myPos">自身の位置</param>
    /// <param name="_nextPos">次の移動位置</param>
    /// <param name="_range">半径</param>
    /// <returns></returns>
    bool CheckDistance(Vector3 _myPos, Vector3 _nextPos, float _range)
    {
        bool inRange = false;
        //targetの位置に近づいたらtrue
        if (Vector3.Distance(_myPos, _nextPos) <= _range)
        {
            inRange = true;
        }
        return inRange;
    }

    /// <summary>
    /// 次のパスに指定する
    /// </summary>
    void NextDesignatedLocation()
    {
        switch (moveType)
        {
            case MoveType.MoveOn:
                pathValue = nextPathValue;
                nextPathValue++;
                if (nextPathValue == movePath.Length-1)
                {
                    moveType = MoveType.Return;
                }
                break;
            case MoveType.Return:
                pathValue = nextPathValue;
                nextPathValue--;
                if (nextPathValue == 0)
                {
                    moveType = MoveType.MoveOn;
                }
                break;
            default:
                pathValue = nextPathValue;
                nextPathValue++;
                if (nextPathValue == movePath.Length - 1)
                {
                    moveType = MoveType.Return;
                }
                break;
        }
    }

まとめ

急いで書いたとはいえ汎用性がなさそうなものができてしまった…

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