Lua将自身上移(Y)5个值?

我只想立刻将自己的位置向上提高5个值,但这段代码也将我(X和Z)推到另一个相同的已记住位置。你看到代码有什么问题吗?

function Behavior:Awake()
local pos = self.gameObject.transform:GetLocalPosition()
self.posy = pos.y

if CS.Input.WasButtonJustPressed("sobe1") then
self.gameObject.transform:SetPosition( Vector3:New( self.posy + 5 ) )
点赞
用户3342050
用户3342050

你的 Behavior:Awake() 函数被调用了,很可能是当你起床或者触发那个事件时。

那些 self.posx.posy&.posz 的值被存储在你的角色表里,直到你在 Behavior:Update() 函数中再次访问它们。

你需要在需要这些值的时候调用 :getLocalPosition() 方法。所以尝试去掉 :Awake() 部分,将这些坐标合并到你的更新向量例程中。

function Behavior:Update()
    if CS.Input.WasButtonJustPressed('sobe1') then
        local pos = self.gameObject.transform:GetLocalPosition()
        self.gameObject.transform:SetPosition( Vector3:New( pos.x, pos.y +4.78, pos.z ) )
    end  --  'sobe1'
end  --  Update()
2020-12-27 13:00:21