Roblox:如何编写 VectorForce 脚本
Roblox: how to script VectorForce
我刚接触 Roblox(2 天)Lua,所以请不要咬我太紧.. =)
我想做的是为我也从代码实例化的部分编写 VectorForce 脚本。
模拟中Attachment和VectorForce确实创建了,但是没有任何预期的效果。
拜托,看看我的脚本,告诉我哪里需要挖掘。
local sandblock = script.Parent
local sandblock_health = 5
local function blockJump(object)
local jump_att = Instance.new("Attachment", object)
local jump_force = Instance.new("VectorForce", object)
jump_force.ApplyAtCenterOfMass = true
jump_force.Attachment0 = jump_att
jump_force.RelativeTo = Enum.ActuatorRelativeTo.World
jump_force.Force = Vector3.new(10,1000,-10)
jump_force.Enabled = true
-- here: what is the difference between Enabled and Active?
--jump_force.Active = true
end
local function onTouch(object)
--print("К блоку прикоснулся "..object.Name)
if object.Name == "Handle" then
sandblock_health = sandblock_health - 1
print(sandblock_health)
if sandblock_health < 1 then
local sandblock_drop1 = Instance.new("Part", workspace)
sandblock_drop1.Size = Vector3.new(2,2,2)
sandblock_drop1.Position = sandblock.Position + Vector3.new(0,5,-1)
blockJump(sandblock_drop1)
sandblock_drop1.Material = "Metal"
sandblock_drop1.BrickColor = BrickColor.new("Gold")
sandblock_drop1.Name = "Gold"
print("Вы добыли "..sandblock_drop1.Name)
sandblock:Destroy()
end
end
end
sandblock.Touched:Connect(onTouch)
问题似乎是当您创建 onTouch
函数时,您有一个参数:object
。但是,当您在触摸该部件的播放器上调用该函数时,您没有输入任何参数:sandblock.Touched:Connect(onTouch)
。要解决此问题,请执行以下操作:sandblock.Touched:Connect(onTouch(<object_parameter>))
解决了。
在我的力矢量中,工作区的重力与零件质量的乘积远高于 1000。
下面的代码按预期工作:
jump_force.Force = Vector3.new(10, game.Workspace.Gravity * object.Mass * 1.35, -10)
jump_force.Enabled = true
wait(0.4)
jump_force.Enabled = false
我刚接触 Roblox(2 天)Lua,所以请不要咬我太紧.. =)
我想做的是为我也从代码实例化的部分编写 VectorForce 脚本。 模拟中Attachment和VectorForce确实创建了,但是没有任何预期的效果。
拜托,看看我的脚本,告诉我哪里需要挖掘。
local sandblock = script.Parent
local sandblock_health = 5
local function blockJump(object)
local jump_att = Instance.new("Attachment", object)
local jump_force = Instance.new("VectorForce", object)
jump_force.ApplyAtCenterOfMass = true
jump_force.Attachment0 = jump_att
jump_force.RelativeTo = Enum.ActuatorRelativeTo.World
jump_force.Force = Vector3.new(10,1000,-10)
jump_force.Enabled = true
-- here: what is the difference between Enabled and Active?
--jump_force.Active = true
end
local function onTouch(object)
--print("К блоку прикоснулся "..object.Name)
if object.Name == "Handle" then
sandblock_health = sandblock_health - 1
print(sandblock_health)
if sandblock_health < 1 then
local sandblock_drop1 = Instance.new("Part", workspace)
sandblock_drop1.Size = Vector3.new(2,2,2)
sandblock_drop1.Position = sandblock.Position + Vector3.new(0,5,-1)
blockJump(sandblock_drop1)
sandblock_drop1.Material = "Metal"
sandblock_drop1.BrickColor = BrickColor.new("Gold")
sandblock_drop1.Name = "Gold"
print("Вы добыли "..sandblock_drop1.Name)
sandblock:Destroy()
end
end
end
sandblock.Touched:Connect(onTouch)
问题似乎是当您创建 onTouch
函数时,您有一个参数:object
。但是,当您在触摸该部件的播放器上调用该函数时,您没有输入任何参数:sandblock.Touched:Connect(onTouch)
。要解决此问题,请执行以下操作:sandblock.Touched:Connect(onTouch(<object_parameter>))
解决了。 在我的力矢量中,工作区的重力与零件质量的乘积远高于 1000。
下面的代码按预期工作:
jump_force.Force = Vector3.new(10, game.Workspace.Gravity * object.Mass * 1.35, -10)
jump_force.Enabled = true
wait(0.4)
jump_force.Enabled = false