roblox基础开发【一】

设置游戏内时间变换

在服务器下添加脚本


设置10s内日夜交替代码.png
local lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")


lighting.ClockTime = 6

local duration = 10
local function cycle(delta)
    lighting.ClockTime = lighting.ClockTime + (delta / duration) * 24   
end

local conn = RunService.Heartbeat:Connect(cycle)
wait(5)
conn:Disconnect()
print("done")

玩家对象脚踩触发灯的开关

放置两个方块部件,并且设置两个方块都为锚固。
其中一块为玩家脚踩触发区域为其添加脚本并设置透明度为0.8,另一块添加PointLight属性设置透明度为1。取消两个方块的碰撞属性。


改变透明度和取消碰撞.png

两个方块部件及其属性.png

时间设置早晨六点时的触发效果.png

脚踩触发的脚本代码如下。

local area = script.Parent
--local light = workspace.bulb.PointLight
local light = workspace:FindFirstChild("bulb").PointLight

local connection

local function LightOn()
    connection:Disconnect()
    light.Enabled = true
    wait(3)
    light.Enabled = false
    area.Touched:Connect(LightOn)
end
connection = area.Touched:Connect(LightOn)

设置按下按键开门程序

放置两个方块模型,其中一个设置为镂空,组合出一个门框。设置两个方块都为锚固。
放置一个方块模型,放置到门中,作为门,设置其为锚固,名称为门。
添加门移动动画脚本,命名为dooropen,代码如下。

  local module = {}

function module.open()
    local TweenService = game:GetService("TweenService")

    local dist = 10
    local door = script.Parent
    local originalCFrame = door.CFrame
    local targetCFrame = originalCFrame * CFrame.new(Vector3.new(0,0,10))

    local duration = 1
    local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
    local tween = TweenService:Create(door, tweenInfo, {CFrame = targetCFrame})

    tween:Play()--播放动画
end

return module

设置开门方式一:点击开门。door下添加ClickDector组件,组件下添加脚本OpenClick。脚本代码如下。

local openModule = require(script.Parent.Parent.DoorOpen)

local click = script.Parent
click.MouseClick:Connect(function()
    openModule.open()
end)

开门方式二:按下键盘按键开门,添加ProximityPrompt组件,在组件下面添加脚本Prompt,代码如下。

local prompt = script.Parent
local openModule = require(script.Parent.Parent.DoorOpen)

local conn
conn = prompt.Triggered:Connect(function()
    openModule.open()
    prompt:Destroy()
    --conn:Disconnect()
end)
开门前效果.png

开门后效果.png

door下面脚本结构.png

实现捡物品

在Workspace添加Tool部件,在tool放置一个方块handle,为它设置表面材质,设为锚固。在方块下添加脚本。代码如下。

local Players = game:GetService("Players")
local brick = script.Parent

brick.Touched:Connect(function(otherPart)
    if otherPart.Parent:FindFirstChild("Humanoid") then
        local p = Players:GetPlayerFromCharacter(otherPart.Parent)
        brick.Parent.Parent = p.Backpack
    end
end)

砖块道具.png

捡到背包里.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容