@dave1707, thank you for that code. There was one other problem that I did encounter. Every time I want to destroy one of the spheres by hitting an object, it sometimes doesn’t remove any of the spheres. Is there anyway to fix this problem??
Here’s my code:
-- Bean
function setup()
-- Create a new craft scene
scene = craft.scene()
scene.sky.material.sky = color(0, 240, 255)
scene.sky.material.horizon = color(0, 131, 255)
scene.sky.material.ground = color(0, 131, 255)
parameter.watch("bean.rotation.x")
parameter.watch("bob[bc].name")
viewer.mode = FULLSCREEN
bobActive = false
pm = false
hit = false
swingshot = false
spawned = false
cr = 0
tr = 0
tp = vec2(0, 0)
br = 0
br2 = 0
bc = 0
hc = 0
nbr = 4
bobs = {}
moveDir = vec3(0, 0, 0)
weapons = {sword = craft.model(asset.builtin.CastleKit.sword_obj);
gun = craft.model(asset.documents.Dropbox.machinegun_obj);
blaster = craft.model(asset.documents.Dropbox.blasterB_obj)}
forward = scene.camera.forward * moveDir.z
right = scene.camera.right * moveDir.x
up = vec3(0,1,0) * moveDir.y
finalDir = forward + right + up
-- Create a new entity
bean = scene:entity()
b = bean:add(craft.rigidbody, DYNAMIC)
b.sleepingAllowed = false
b.angularDamping = 0.4
bean.model = craft.model(asset.builtin.Primitives.Capsule)
bean:add(craft.shape.model, bean.model)
bean.position = vec3(0, -1, 0)
bean.scale = vec3(2, 2, 2) / 8
bean.material = craft.material(asset.builtin.Materials.Standard)
lob = scene:entity()
lb = lob:add(craft.rigidbody, STATIC)
lb.sleepingAllowed = false
lob.model = craft.model(asset.builtin.Primitives.Sphere)
lob:add(craft.shape.model, lob.model)
lob.position = vec3(bean.x + 1, bean.y, bean.z)
lob.scale = vec3(1, 1, 1) / 8
lob.material = craft.material(asset.builtin.Materials.Standard)
createGround(vec3(0, -3.12, 0))
scene.camera.position = vec3(-5, 2.5, -5)
scene.camera.eulerAngles = vec3(25, 45, 0)
cameraSettings = scene.camera:get(craft.camera)
cameraSettings.ortho = true
cameraSettings.orthoSize = 2.5
end
function createGround(p)
ground = scene:entity()
g = ground:add(craft.rigidbody, STATIC)
g.restitution = 1
ground.model = craft.model.cube(vec3(4, 4, 4))
ground:add(craft.shape.box, vec3(4, 4, 4))
ground.position = p
ground.material = craft.material(asset.builtin.Materials.Standard)
ground.material.map = readImage(asset.builtin.Blocks.Sand)
end
function createSubject(p)
bob[bc] = scene:entity()
bob[bc].name = "Bob No."..bc
bob[bc].model = craft.model(asset.builtin.Primitives.Sphere)
bob[bc].position = p
bob[bc].scale = vec3(1, 1, 1) / 8
bob[bc].material = craft.material(asset.builtin.Materials.Standard)
bc = bc + 1
end
function createSubjects(n)
for i=1, n do
llob = scene:entity()
l = llob:add(craft.rigidbody, DYNAMIC)
llob:add(craft.shape.sphere, 1, 3)
l.sleepingAllowed = false
l.restitution = 0.2
llob.name = "Bob No."..bc
llob.model = craft.model(asset.builtin.Primitives.Sphere)
llob.position = vec3(bean.x - 1, -1, 0)
llob.scale = vec3(1, 1, 1) / 8
llob.material = craft.material(asset.builtin.Materials.Standard)
end
table.insert(bobs, 1)
for a,b in pairs(bobs) do
llob.x = math.random(-3, 3) * .2
llob.y = 0
llob.z = math.random(-1, -1) * .2
end
end
function weaponGen()
weapon = scene:entity()
weapon.model = craft.model()
end
function coinGen(p)
coin = scene:entity()
coin.model = craft.model(asset.documents.Dropbox.coinGold_obj)
coin.position = p
coin.eulerAngles = vec3(0, 0, 0)
coin.scale = vec3(1, 1, 1) / 8
end
function BD(n)
if llob[n] ~= nil then
llob[n]:remove(craft.model)
print("Bob No."..n.." destroyed!")
else
print("No such Bob exists!")
end
end
function BP(n, a)
if llob[bc].name == "Bob No."..n then
llob[n].model = a
end
end
function update(dt)
-- Update the scene (physics, transforms etc)
scene:update(dt)
bean.eulerAngles = vec3(0, 0, 0)
end
-- Called automatically by codea
function draw()
update(DeltaTime)
-- Draw the scene
scene:draw()
calc()
scene.debug:line(vec3(bean.x, bean.y, bean.z), vec3(lob.x, lob.y, lob.z), color(255))
ray = scene.physics:raycast(vec3(bean.x, bean.y, bean.z), vec3(lob.x, lob.y, lob.z), 100)
if ray ~= nil then
text(ray.entity.z, 500, 500)
end
end
function touched(touch)
if touch.state == BEGAN or touch.state == MOVING then
b.friction = 0.4
end
if touch.x < WIDTH/2 then
if touch.state == MOVING and touch.deltaY > 0 then
b:applyForce(vec3(0, 0, 7))
pm = true
end
if touch.state == MOVING and touch.deltaY < 0 then
b:applyForce(vec3(0, 0, -7))
pm = true
end
if touch.state == MOVING and touch.deltaX > 0 then
b:applyForce(vec3(-7, 0, 0))
if br <= 25 then
br = br + 5
end
pm = true
end
if touch.state == MOVING and touch.deltaX < 0 then
b:applyForce(vec3(7, 0, 0))
if br >= -25 then
br = br - 5
end
pm = true
end
end
if touch.x > WIDTH/2 then
if touch.state == MOVING and touch.deltaX > 0 then
cr = cr + 2
pm = true
end
if touch.state == MOVING and touch.deltaX < 0 then
cr = cr - 2
pm = true
end
end
if touch.x > WIDTH/2 then
if touch.tapCount == 2 then
if touch.state == BEGAN then
sound(SOUND_JUMP, 3209)
b.linearVelocity = vec3(0, 3, 0)
b.linearDamping = 0.4
end
end
end
if touch.tapCount == 2 then
if touch.state == BEGAN then
bc = bc + 2
createSubjects(3)
if bc == 1 then
print("There is "..bc.." Bob in the game!")
else
print("There are "..bc.." Bobs in the game!")
end
end
end
if touch.state == ENDED then
pm = false
b.angularVelocity = vec3(0, 0, 0)
if swingshot == true then
b.linearVelocity = vec3(lob.x - math.ceil((lob.x - 100)/12), lob.y - math.ceil((lob.y - 100)/12), lob.z - math.ceil((lob.z - 150)/12))
b.linearDamping = 0.4
b.friction = 2
end
end
end
function calc()
x = math.abs(bean.x - lob.x)
y = math.abs(bean.y - lob.y)
z = math.abs(bean.z - lob.z)
if x<.2 and y<.2 and z<.2 then
hit = false
if hit == false then
print("HIT!")
sound(SOUND_HIT, 21739)
b:applyForce(vec3(3, 0, 3))
b.linearVelocity = vec3(0, 5, 0)
b.linearDamping = 0.4
for a,b in pairs(bobs) do
llob:destroy()
spawned = false
end
bc = bc - 1
if bc == 1 then
lob:remove(craft.model)
end
if bc > 0 then
print("One in "..bc)
if bc == 1 then
print("There is now "..bc.." Bob in the game!")
else
print("There are now "..bc.." Bobs in the game!")
end
end
end
hit = true
end
end
Thanks 