In the game that I am making, I want to use raycast to find an object with a rigid body attached to it, but I don’t know what code to write if a rigid body is found, and if so, what to write next. Here’s the code that I have done so far:
– Fruit & Blobs
—function setup()
– Create a new craft scene
scene = craft.scene()
scene.physics.gravity = vec3(0, 0, 0)
day = readText(asset.builtin.Environments.Sunny)
env = craft.cubeTexture(json.decode(day))
scene.sky.material.envMap = env
cameraYPos = {x = 0, y = 0, z = 0}
playerXPos = 0
viewer.mode = FULLSCREEN
-- Create a new entity
local e = scene:entity()
body = e:add(craft.rigidbody)
e.model = craft.model(asset.builtin.Primitives.Capsule)
e.x = 0.15
e.y = 0
e.z = -2
e.scale = vec3(1, 1, 1) / 8
scene.camera.z = -0.1
end
—function touched(touch)
if touch.state == MOVING then
scene.camera.eulerAngles = vec3(0, cameraYPos.y, 0)
cameraYPos.y = cameraYPos.y + -1
playerXPos = playerXPos + 1
end
if touch.state == BEGAN then
scene.physics:raycast(vec3(0, 0, 0), vec3(0, 0, 5), 100)
end
end
—function update(dt)
– Update the scene (physics, transforms etc)
scene:update(dt)
end
– Called automatically by codea
—function draw()
update(DeltaTime)
-- Draw the scene
scene:draw()
end