Firstly I am *not complaining about the tutorials - they are great !
For tutorial #3, when you pick a planet up, it gets a little bigger, and then when you drop it, it returns to normal size.
However *sometimes it does not return to normal size, and stays enlarged.
You can even see it happen at the start of the video !
I’ll attach a project.
I’m confused about why this happens, looking at the code it seems ok ?
Any thoughts ?
I haven’t looked at the code but you mentioned that it gets bigger. Is it an animation? It could be that the animation gets cancelled before completed?
-- Revise script control variables; click/touch -- as pressed or released
if action.pressed then
-- When the action reflects a press
msg.post("#collisionobject", "enable")
self.pressed = true
elseif action.released then
-- When the action reflects a release
msg.post("#collisionobject", "disable")
self.pressed = false
self.dragged_id = nil
self.dragging = false
-- Reset the scale to 1.0 on Selected GameObject when released
local s1 = vmath.vector3(1.0, 1.0, 1.0)
go.set_scale(s1, self.collision_id)
end
So it looks like the scale command gets missed somehow ?
Since the only thing affecting the scale of any nodes within the provided script is the set scale, so I imagine that either the nodes are set to scales of less than vmath.vector3(1.0, 1.0, 1.0) in the relevant collection or that there is code elsewhere that also alters the scale. If you could send me your project file, I could find the problem and give you a fix
Here is the entire script, the scale is changed in two places looks like:
function on_input(self, action_id, action)
-- Only process inputs which reflect a 'touch action' as defined within game.input_bindings
if not action_id or action_id == hash("touch") then
-- Update the Defold cursor Game Object position to be the same as the current
-- position of the machine cursor
local action_pos = vmath.vector3(action.x, action.y, 0)
go.set_position(action_pos)
-- Revise script control variables; click/touch -- as pressed or released
if action.pressed then
-- When the action reflects a press
msg.post("#collisionobject", "enable")
self.pressed = true
elseif action.released then
-- When the action reflects a release
msg.post("#collisionobject", "disable")
self.pressed = false
self.dragged_id = nil
self.dragging = false
-- Reset the scale to 1.0 on Selected GameObject when released
local s1 = vmath.vector3(1.0, 1.0, 1.0)
go.set_scale(s1, self.collision_id)
end
-- Upon a Click/Touch -- if the cursor has collided with another GameObject and
-- is not already dragging another GameObject -- then set dragged_id / dragged_pos
-- to the new GameObject's id
if self.pressed and self.collision_id and not self.dragged_id then
self.dragged_id = self.collision_id
self.dragged_pos = action_pos
-- Increase Scale for selected GameObject when selected
local s2 = vmath.vector3(1.2, 1.2, 1.0)
go.set_scale(s2, self.collision_id)
end
-- Set self.dragging to true if user has clicked a GameObject and moves it
if self.dragged_id and vmath.length(self.dragged_pos - action_pos) > 20 then
self.dragging = true
end
-- Update the position of a dragged GameObject
if self.dragging then
go.set_position(action_pos, self.dragged_id)
end
end
elseif action.released then
-- When the action reflects a release
msg.post("#collisionobject", "disable")
self.pressed = false
self.dragging = false
-- Reset the scale to 1.0 on Selected GameObject when released
local s1 = vmath.vector3(1.0, 1.0, 1.0)
go.set_scale(s1, self.dragged_id)
self.dragged_id = nil
end
the alterations are: move self.dragged_id = nil to below go.set_scale and modify the second parameter of go.set_scale to “self.dragged_id”
This bug occurred because the previous code was, when the mouse was released, altering whatever game object collided first, which in this case would always be the one on top. This fix solves this problem by storing and referencing a specific game object id, and taking the collision out of the action.released
I probably should have posted the whole script in the first place
Here is a diff
old
elseif action.released then
-- When the action reflects a release
msg.post("#collisionobject", "disable")
self.pressed = false
self.dragged_id = nil
self.dragging = false
-- Reset the scale to 1.0 on Selected GameObject when released
local s1 = vmath.vector3(1.0, 1.0, 1.0)
go.set_scale(s1, self.collision_id)
end
new
elseif action.released then
-- When the action reflects a release
msg.post("#collisionobject", "disable")
self.pressed = false
self.dragging = false
-- Reset the scale to 1.0 on Selected GameObject when released
local s1 = vmath.vector3(1.0, 1.0, 1.0)
go.set_scale(s1, self.dragged_id)
self.dragged_id = nil
end