Im trying to adapt @John’s first-person player-control system, the one from the built-in examples.
In the original code, you can move the player figure around and it will do an automatic jump whenever it hits a Craft block, but I must’ve gone wrong somewhere in my adaptation, because it seems to jump sometimes just from simple contact with the ground.
I’ve isolated the relevant code into the attached project, and it’s also available for cut-and-paste right here:
viewer.mode = FULLSCREEN
function setup()
assert(blocks, "Project requires Block"..
" Library as a dependency")
makeScene()
makeGround()
makeOrc()
makeTouchesTriggerForce()
end
function makeScene()
craft.scene.main = craft.scene()
craft.scene.main.physics.gravity =
vec3(0,-14.8, 0)
viewer = craft.scene.main.camera:add(
FirstPersonViewer)
craft.scene.main.camera.position =
vec3(29.5, 14, 54)
viewer.rx = 10
end
function makeGround()
-- Setup voxel terrain.
-- Because of the way Block Library works,
-- making blocks requires a "scene" global
scene = craft.scene.main
_ = blocks()
local voxels = craft.scene.main.voxels
voxels:resize(vec3(5,1,5))
voxels.coordinates = vec3(0,0,0)
-- Create ground out of grass
voxels:fill("Planks")
voxels:box(0,10,0,16*5,10,16*5)
--something to bump into, for testing jumps
voxels:fill("Red Brick")
voxels:box(20,11,30, 50,11,60)
voxels:fill("empty")
voxels:fillStyle(REPLACE)
voxels:box(21,11,31, 49,11,59)
output.clear()
end
function makeOrc()
--create orc
orc = craft.scene.main:entity()
orc.model = craft.model(asset.builtin.
Blocky_Characters.Orc)
orc.position = vec3(29.5, 20, 66.5)
orc.scale = vec3(0.12485, 0.12485, 0.12485)
--add rigidbody the same size as the orc
rb = orc:add(craft.rigidbody, DYNAMIC, 1)
local size = orc.model.bounds.size
size.x = size.x * orc.scale.x
size.y = size.y * orc.scale.y
size.y = size.z * orc.scale.z
orc:add(craft.shape.capsule, size.x,
size.y)
--set default rigidbody attributes
rb.angularFactor = vec3(0,0,0)
rb.sleepingAllowed = false
rb.friction = 0.5
rb.group = 1<<11
rb.linearDamping = 0.97
end
--utility class for making touch handlers
TouchReceiver = class()
function TouchReceiver:init()
touches.addHandler(self, -1, true)
end
--make "shouldApplyForce" true during touches
function makeTouchesTriggerForce()
--make a touch receiver
local touchToMove = TouchReceiver()
--override its touched function
function touchToMove:touched(touch)
if touch.state ~= CANCELLED and
touch.state ~= ENDED then
force = 10
if touch.x > WIDTH / 2 then
force = -10
end
shouldApplyForce = true
else
shouldApplyForce = false
end
end
end
function draw()
--onscreen instructions
text("Let orc come to a full stop, then",
WIDTH / 2, HEIGHT - 65)
text("tap to apply force left or right.",
WIDTH / 2, HEIGHT - 90)
text("Why is jump triggered so much?",
WIDTH / 2, HEIGHT - 115)
--apply force if shouldApplyForce
if shouldApplyForce then
if rb.linearVelocity.x < 5 and
rb.linearVelocity.x > -5 then
rb:applyForce(vec3(force, 0, 0))
end
end
--make orc jump if it hits something
if orcHitSomething() then
jump()
end
end
--use sphereCast to detect orc collisions
function orcHitSomething()
local hitCheck = craft.scene.main.
physics:sphereCast(
orc.position, vec3(0,-1,0), 0.5,
0.52, ~0, ~rb.group)
local didHit = hitCheck and
hitCheck.normal.y < 0.5
return didHit
end
--apply momentary boost of vertical force
function jump()
print("boing")
local v = rb.linearVelocity
v.y = 3.5
rb.linearVelocity = v
end
Can anyone help me stop the figure from jumping all the dang time, so it works like it does in the built in example?