unity 2d 入门 飞翔小鸟 柱子移动(十一)

c#脚本

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoleMove : MonoBehaviour
{
    //移动上限制
    public float up;
    //移动下限
    public float below;
    //速度
    private float speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = Random.Range(0.5f, 2.5f);
    }

    // Update is called once per frame
    void Update()
    {
        if (transform.position.y>up)
        {
            speed = -speed;
        }
        if (transform.position.y < below)
        {
            speed = -speed;
        }
        //让柱子上限移动
        transform.position = new Vector3(transform.position.x, transform.position.y + speed * Time.deltaTime, transform.position.z);
    }
}

创建空的图层,并把柱子拖拽到同个图层下

给pole拖拽添加脚本

相关推荐
三和尚13 分钟前
AI开发之Cursor的下载安装以及Unity-MCP下载安装到你的个人Unity项目中(一)
unity·ai·游戏引擎·cursor·unity-mcp·unity自动化
__water44 分钟前
RHQ《Unity2D图片切割方式》
unity·2d·精灵图切割
DaLiangChen2 小时前
Unity 导览相机实现:键鼠控制自由漫游(WASD 移动 + 右键旋转)
数码相机·unity·游戏引擎
沉默金鱼17 小时前
Unity实用技能-UI进度条
ui·unity·游戏引擎
老朱佩琪!1 天前
Unity离线开发经验分享
unity·游戏引擎
Sator11 天前
unity的GPUInstance和GPU动画
unity·游戏引擎
沉默金鱼2 天前
Unity实用技能-模型
unity·游戏引擎
阿里云云原生2 天前
AgentRun:如何利用 AI Agent 构建现代化的舆情分析解决方案?
人工智能·unity·游戏引擎
在路上看风景2 天前
2.8 预渲染
unity
老朱佩琪!2 天前
Unity代理模式
unity·游戏引擎·代理模式