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拖拽添加脚本

相关推荐
XR-AI-JK6 小时前
Unity VR/MR开发-VR/开发SDK选型对比分析
unity·vr·mr
心之所向,自强不息13 小时前
【Unity Shader编程】之让画面动起来
unity·游戏引擎
不伤欣1 天前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
Magnum Lehar1 天前
vulkan游戏引擎test文件memory实现
游戏引擎
Magnum Lehar1 天前
vulkan游戏引擎test_manager实现
java·算法·游戏引擎
快乐觉主吖2 天前
Unity的日志管理类
android·unity·游戏引擎
WarPigs2 天前
Unity性能优化笔记
笔记·unity·游戏引擎
T.D.C2 天前
【业务框架】3C-相机-Cinemachine
unity
一线灵3 天前
跨平台游戏引擎 Axmol-2.6.1 发布
游戏引擎
Clank的游戏栈3 天前
Unity基于GraphView的可视化关卡编辑器开发指南
unity·编辑器·游戏引擎