04 跑酷遊戲
(2023/10/01)
開始佈置跑酷平台之前
我習慣把【重生點】做如下的調整
執行後,就知道主角朝的方向(前方)
或事後再調整【重生點】的方向也可以
佈置跑酷平台
利用之前學過的東西來製作
增加更多物件
區塊
球體
楔形
角落楔體
圓柱
調整物置
選擇
移動
縮放
旋轉
改變顏色
顏色
把物件固定
固定
物件數量約10~20個,完成如下
二、簡單編寫程式碼
在一個區塊,增加Script(腳本或程式)
自動產生 print("Hello world!")的程式
常見指令
print 印出、列出
print("Hello Roblox") 文字要有雙引號(或單引號)
print(123) 數字不用雙引號
wait 等待
wait(10) 後面數字為秒數,等待10秒
註解
-- 大家都是帥鴿美女 前面有--(2個減號)代表這是註解,不執行此行
使用變數
創建變數的時候要在前面加上【local】
local test = 1 (宣告時才要加local,最初的值為1)
test = 2
print(test)
創建函數
創建函數就像Scratch的自建積木,開頭加上【function 名稱()】,名稱後面要加()
不帶參數
local test = 1 (宣告時才要加local,最初的值為1)
function my() 建一個名稱為my的函數
test = 2
print(test)
end
my()
有帶參數
local test = 1 (宣告時才要加local,最初的值為1)
function my(abc) 建一個名稱為my的函數
print(abc)
end
my(100)
my(test)
也可以帶多個參數
三、加入物件概念
認識物件
Script和區塊間的關係
Script是區塊的下一個層級,所以具有從屬關係,這種關係就稱為親子關係(Parent)
物件表示法
script 就是物件本身(Script本身)
script.Parent 就是Script的父級,也就是【區塊】的意思
物件(Object)的特性:
屬性(Property):指物件各種特性的現況,物件擁有許多特性(attribute),這些特性代表了一個物件的外觀或某些性質。(如是否碰撞、顏色、大小都是屬性 )
方法(Method):每個物件都擁有不同的行為,這些行為在物件導向程式設計中稱之為方法(Method)指物件的功能。
回到這跑酷遊戲的終點線
【終點線】的區塊及Script名稱說明
區域名稱:GoalLine
Script名稱:GoalScript
用變數來管理終點線
local goalLine = script.Parent (終點線的區塊)
使用事件
當人物碰到GoalLine時,就判定為抵達終點
想要判定物件被什麼東西碰到的話,這種機制就是【事件】
事件的功能是:當發生特定的情況時,就執行預先登錄的函數
舉例來說:在地上放置香蕉皮,有人踩在上面就會跌倒
若要判定被什麼東西觸碰到的話,就要使用【Touched】這一事件
goalLine.Touched (代表這物件被觸碰到了)
goalLine.Touched :Connect()(代表這物件被觸碰到了,並連結到某函數)
完整程式
local goalLine = script.Parent
function onTouched(hit)
print("Goal!!")
end
goalLine.Touched :Connect(onTouched)
物件名稱.事件名稱:要登錄的函數名稱
只有人物觸碰時發生判定
local goalLine = script.Parent
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
print("Goal!!")
end
end
goalLine.Touched :Connect(onTouched)
上面程式補充說明
人物的物件,即為自己帳號名稱的物件,名稱為【Humanoid】
執行遊戲的狀態下,可在總管中進行人物的查看與確認
這個Humanoid物件用來管理人物的生命值或走路速度等屬性
以上說明如下圖
尋找並取得觸碰到Trap的人物的Humanoid物件!只有在人物觸碰時,變數humanoid的內容才有東西的狀態!
各個區塊增加一些變化
生命值歸零
碰到某區塊時,【生命值歸零】
區域名稱:Trap(陷阱)
Script名稱:TrapScript
用變數來管理Trap(陷阱)
local trap = script.Parent (陷阱的區塊)
只有人物觸碰時發生判定
local trap = script.Parent
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
trap.Touched :Connect(onTouched)
變更跳躍力
碰到某區塊時,【變更跳躍力】
區域名稱:Jump(跳)
Script名稱:JumpScript
用變數來管理Jump(跳)
local jump = script.Parent (變更跳躍力的區塊)
只有人物觸碰時發生判定
local jump = script.Parent
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.JumpHeight = 30
end
end
jump.Touched :Connect(onTouched)
碰到該方塊後,就擁有更高的跳躍能力(原來高度?)
如果高度設為0,碰到這方塊後,就是禁止跳躍
會消失的區塊
碰到某區塊時,【區塊會消失】
區域名稱:Fall(掉下)
Script名稱:FallScript
用變數來管理fall
local fall = script.Parent (會消失的區塊)
CanCollide 為屬性(Property),可以用來設定物件是否執行碰撞判斷。
當CanCollide 這屬性為True時,代表啟用碰撞判定;當False時,代表關閉碰撞判定,物體之間會穿過彼此而不會碰撞
fall.CanCollide = false 就是代表如果人物在其上方的話,人物就會掉入深淵
讓區塊有時碰撞有效、有時無效
local fall = script.Parent
while true do
fall.CanCollide = false
wait(3)
fall.CanCollide = true
wait(3)
end
但這時外觀是看不出這區塊有沒有效
透過透明度(Transparency)的屬性來設定其透明度
local fall = script.Parent
while true do
fall.CanCollide = false
fall.Transparency = 0.8 --快透明了
wait(3)
fall.CanCollide = true
fall.Transparency = 0 --完全不透明
wait(3)
end
會移動的區塊
【區塊會移動】
區域名稱:Move
Script名稱:MoveScript
用變數來管理
local move = script.Parent
讓區塊左右移動
local move = script.Parent
while true do
move.Position += Vector3.new(1,0,0) --X方向移動1
wait(1)
move.Position += Vector3.new(-1,0,0) --X方向移動-1
wait(1)
end
說明:
Position由X、Y、Z,這3組座標組合而成,想要移動時,只要對當前的Position進行加法即可
move.Position = move.Position + Vector3.new(1,0,0) 和 move.Position += Vector3.new(1,0,0) 是一樣的
顯示時間
當碰到終點區塊時,【會顯示所花費的時間】
區域名稱:GoalLine(終點線,前面已做過)
Script名稱:GoalScript
用變數來管理
local goalLine = script.Parent (終點的區塊)
local isGoal = false (再建一個變數)
只有人物觸碰時發生判定
local goalLine = script.Parent
local isGoal = false
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if isGoal == false then
print("Goal!!")
isGoal = true
print(time())
end
end
end
goalLine.Touched :Connect(onTouched)
time()是遊戲後經過了多少時間(單位為秒)
isGoal是一個控制只會顯示一個次時間的變數
做出效果
當碰到終點區塊時,【會有火花的效果】
這邊是使用Instance.new()的函數,把想要使用的效果放入new()中,可從總管知道有哪些效果可用(下一單元)
要讓效果出現在遊戲世界中,只要使其成為遊戲中既有的物件之子級就可以實現這功能了。
local effect = Instance.new("Sparkles")
effect.Parent = goalLine --將goalLine設定為effect的父級
只有人物觸碰時發生判定
local goalLine = script.Parent
local isGoal = false
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if isGoal == false then
print("Goal!!")
isGoal = true
local effect = Instance.new("Sparkles")
effect.Parent = goalLine
print(time())
end
end
end
goalLine.Touched :Connect(onTouched)
直接利用總管來添加效果
追加語音
當遊戲開始,需要播放音樂
當碰到終點區塊時,需要播放音樂
開啟【工具箱】,如果沒有出現(檢視/工具箱)
找到【語音】
搜尋音樂
想要一開始遊戲,就自動播放所選擇的音樂(或音效)
目前,我加入語音時,就出現Fail to Load sound....的問題
如果想要一開始遊戲,就自動播放所選擇的音樂,要把
Playing 打勾
如果想要到達終點出現音樂
先點擊終點物件(方塊)
再用同樣的方式加入音樂
終點方塊的程式,如下:
local goalLine = script.Parent
local isGoal = false
local sound = goalLine["The Golden Goal"]
function onTouched(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
if isGoal == false then
print("Goal!!")
print(time())
isGoal = true
local effect = Instance.new("Fire")
effect.Parent = goalLine
sound:Play()
end
end
end
goalLine.Touched :Connect(onTouched)
使用工具箱的模型
想要使用內建的模型時
開啟【工具箱】,如果沒有出現(檢視/工具箱)
找到【模型】
搜尋模型