In the Defold game engine, when you perform a raycast, you can retrieve information about what the ray hit in the response table that is provided in the callback function. One of the fields in this table is id, which is the identifier of the game object that was hit by the raycast.
To use a raycast and get the game object ("go") from the raycast results, follow these steps within a Lua script attached to a game object in Defold:
Require the physics module:
local physics = require "physics"
User
result.id is nil
Assistant
If result.id is nil when you're performing a raycast in Defold, it means that no game object with a collision object was hit by the raycast. This could be due to several reasons, such as:
The raycast didn't intersect with any collision objects: It's possible that the starting (from) and ending (to) points of your raycast don't actually cross any collision objects in your scene.
User
result is not nil, but result.id is nil
Assistant
If result is not nil, but is in the raycast callback in Defold, this behavior can occur if the ray hits a collision object that is not associated with a game object. In Defold, it is possible for collision objects to exist without a game object, such as when using tilemap collisions. When a raycast hits such a collision, there will be a , but will be .
Perform the raycast:
local from = vmath.vector3(0, 0, 0) -- Starting point of the ray
local to = vmath.vector3(100, 0, 0) -- Ending point of the ray
local mask = hash("your_collision_group") -- Set the collision group
physics.raycast(from, to, mask, function(self, result)
if result then
local hit_go = result.id -- This is the game object that was hit by the raycast
-- Do something with the hit game object
print("Raycast hit: " .. tostring(hit_go))
else
print("Raycast did not hit anything")
end
end)
Make sure you specify the correct collision group (mask) that the ray should check against.
In the example above, result.id contains the instance ID of the game object hit by the raycast. You can use go.get_position(result.id) or other go functions to manipulate the game object as needed, or access its properties.
Please make sure that the layer (collision group) your raycast is checking against is set up correctly in your game.project file and that the game objects you want to be detectable by the raycast all belong to the specified collision group.
Remember to also make sure that any game objects that should be possible to hit by a raycast have collision objects attached to them, and those collision objects must belong to the correct collision group (the same as the one you specify when performing the raycast).
Incorrect collision groups: Ensure the collision object you want to hit with the raycast is part of the collision group (mask) you are using in your raycast.
Collision objects not set up properly: Double-check that the collision objects are set up with the correct collision shapes and that they're enabled.
Raycast setup issue: Verify that the raycast is correctly set up with properly configured from, to, and mask parameters.
Here’s what you should double-check:
Verify the raycast from and to coordinates.
Make sure that the collision group specified in the raycast call matches one of the groups set in the collision object.
Check that the collision object associated with the game object is active and enabled in the scene.
An example raycast setup might look like this:
local function raycast_result(self, hit)
if hit then
print("Raycast hit an object", hit.id)
else
print("Raycast did not hit anything")
end
end
function init(self)
local from = vmath.vector3(0, 0, 0)
local to = vmath.vector3(100, 0, 0)
local mask = hash("your_collision_group") -- This must match the collision group in the editor
physics.raycast(from, to, mask, raycast_result)
end
And in your collision object, within the editor:
Set the Group to your_collision_group.
Add the appropriate Mask values for what the collision object should collide with.
If you've checked all of these things and result.id is still nil, consider creating a minimal reproducible example and posting it on the Defold forums or seeking help, as there might be nuances specific to your project setup that need to be reviewed.
result.id
nil
result
result.id
nil
Here is an example of handling both cases in a raycast callback function:
local function raycast_result(self, hit)
if hit then
if hit.id then
-- A game object with an associated collision object was hit
local hit_go = hit.id
print("Raycast hit a game object with id:", hit_go)
-- You can do something with the hit game object here
else
-- The raycast hit a collision object that is not associated with a game object
print("Raycast hit a collision object that has no game object id")
-- You might handle tilemap or other "environmental" collisions here
end
else
print("Raycast did not hit anything")
end
end
function init(self)
local from = vmath.vector3(0, 0, 0)
local to = vmath.vector3(100, 0, 0)
local mask = hash("your_collision_group") -- This must match the collision group in the editor
physics.raycast(from, to, mask, raycast_result)
end
To resolve this issue:
Ensure game objects have collision objects: If your intention is to hit game objects, ensure that all game objects that you expect to be hit by the raycast have collision objects attached to them and are set up with correct collision groups.
Check the collision groups: The raycast mask should match the collision group of the objects you intend to detect. Verify that the mask passed to physics.raycast is correct.
Handle no id properly: Depending on your game logic, you may want to handle the situation where you hit a collision without a id. For example, if hitting a tilemap layer, you might take different actions compared to hitting a game object.
Verify game object setup: If you expect a particular game object to have an id upon being hit but it does not, double-check that the game object instance indeed has a collision component attached and correctly set up in the editor.
If after all these checks you're still experiencing unexpected behavior, you may want to post on the Defold forums with specific details about your project for community assistance.