Unity3D无限滚动脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wang_lvril/article/details/82621576

效果:


原理:

复制一份组件,放到本体后面,设置endpos为滚动的长度,当滚动到endpos位置,就复位。

脚本:MyFlow

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

public class MyFlow : MonoBehaviour
{
    [Header("=====控制======")]
    public bool start = true;           //是否开启滚动
    public bool horizontal = false;     //是否横向滚动
    public float minSpeed;              //最小速度
    public float maxSpeed;              //最大速度
    private float speed;            
    public float endPos = 1050.0f;      //一个组件长度

    private void Awake()
    {
        speed = Random.Range(minSpeed, maxSpeed);
    }

    void Update()
    {
        if (!start) {
            return;
        }
        Vector2 pos = transform.localPosition;

        if (horizontal)
        {
            pos.x += speed * Time.deltaTime;
            if (pos.x > endPos)
            {
                pos.x = 0;
            }
        }
        else
        {
            pos.y -= speed * Time.deltaTime;
            if (pos.y < -endPos)
            {
                pos.y = 0;
            }
        }
        transform.localPosition = pos;
    }
}

猜你喜欢

转载自blog.csdn.net/wang_lvril/article/details/82621576
今日推荐