Skip to content

Commit

Permalink
Added check for empty urls + changed default values + added multiple …
Browse files Browse the repository at this point in the history
…collision mask support
  • Loading branch information
astrochili committed Jul 29, 2022
1 parent 402e293 commit 227e4e1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ Add link to the zip-archive of the latest version of [defold-kinematic-walker](h

## Quick Start

1. Add the `collision_standing` collision object with the capsule shape to your character's gameobject. The position anchor must be on the floor. Set type to `Kinematic`, set group to `walker` and switch locked rotation to `true`.
1. Add the `collision_standing` collision object with the capsule shape to your character's gameobject. The position anchor must be on the floor. Set type to `Kinematic` and switch locked rotation to `true`.

2. Add `walker.script` to your character's gameobject and configure its script properties in the editor.

### Crouching

To allow crouching add the `collision_crouching` collision object with the capsule shape with the same collision properties as the `collision_standing` but with a lower height.
To allow crouching add the `collision_crouching` collision object with the capsule shape with the same collision properties as the `collision_standing` but with a lower height. Then set the [`is_crouching_allowed`](#is_crouching_allowed) to `true`.

### Camera

Expand All @@ -51,7 +51,7 @@ To follow your camera gameobject rotation:
msg.post(walker_url, hash 'follow_camera', { camera = camera_url })
```

To automatically move the camera up and down when standing and crouching add an empty gameobject `eyes` in the walker gameobject and use it to attach your camera.
To automatically move the camera up and down when standing and crouching add an empty gameobject `eyes` in the walker gameobject and use it to attach your camera. Then set the [`eyes_switching`](#eyes_switching) to `true`.

### Operator

Expand Down Expand Up @@ -108,12 +108,6 @@ How much units of velocity should be changed per second to get a lower velocity.
- `20` - if the `normal_speed` is `5`, then the time required to decelerate from the walking velocity to the zero velocity will be `0.25` seconds.
- `0` - _don't use otherwise you will never stop the walker._

### is_crouching_allowed

Allows to crouch. Be sure that the `collision_crouching` is set.

Use it to temporary disable crouchoing in runtime, for example.

### stair_height

Maximum height of the stair to climb.
Expand Down Expand Up @@ -188,6 +182,10 @@ How much units of velocity should be changed per second to reach the [`gravity`]

The sensor length to check the ground, ceiling and slopes. Minimum value is `0.05` because of the Bullet physics [collision margin](https://forum.defold.com/t/using-a-dae-mesh-for-collision/69434/3).

### is_crouching_allowed

Allows to crouch. Be sure that the `collision_crouching` is set.

### collision_standing

The `url` of the collision object with a standing capsule shape.
Expand All @@ -198,23 +196,19 @@ The `url` of the collision object with a crouching capsule shape.

Is optional if you don't plan to allow crouch.

### walker_collision_group

The collision group you set in the [`collision_standing`](#collision-standing) and [`collision_crouching`](#collision-crouching) collision object properties.
### eyes_switching

### world_collision_group
Animates the [`eyes`](#eyes) gameobject position up and down when standing and crouching.

The collision group you use in the level geometry.
### eyes

### eyes_position_url

The `url` of a gameobject for the camera attachment. Its position will be animated when crouching and standing.
The `url` of a gameobject for the camera attachment. Its position will be animated when the [`eyes_switching`](#eyes_switching) is `true`.

Is optional if you don't plan to switch the camera position.

### eyes_switch_duration

Duration of the `eyes` gameobject position animation when crouching and standing.
Duration of the [`eyes`](#eyes) gameobject position animation when crouching and standing.

## Incoming Messages

Expand All @@ -234,6 +228,14 @@ Enable or disable the [spectator mode](#spectator_mode).
msg.post(walker_url, hash 'spectator_mode', { is_enabled = true } )
```

### collision_mask

Unfortunately, there is no way to get your collision object mask automatically in Defold at the moment. So if you use a mask other than `default` in [`collision_standing`](#collision_standing) and [`collision_crouching`](#collision_crouching), please provide it with this message during initialization.

```lua
msg.post(walker_url, hash 'collision_mask', { hash 'default', hash 'solid' } )
```

### follow_camera

Follow the camera object rotation to apply it on the walker rotation.
Expand Down
10 changes: 10 additions & 0 deletions example/player.collection
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ embedded_instances {
" value: \"#player\"\n"
" type: PROPERTY_TYPE_URL\n"
" }\n"
" properties {\n"
" id: \"is_crouching_allowed\"\n"
" value: \"true\"\n"
" type: PROPERTY_TYPE_BOOLEAN\n"
" }\n"
" properties {\n"
" id: \"eyes_switching\"\n"
" value: \"true\"\n"
" type: PROPERTY_TYPE_BOOLEAN\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"collision_crouching\"\n"
Expand Down
37 changes: 27 additions & 10 deletions walker/walker.script
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ go.property('shift_speed', 5)
go.property('acceleration', 10)
go.property('deceleration', 20)

go.property('is_crouching_allowed', true)

go.property('stair_height', 0.3)
go.property('stair_angle', 15)

Expand All @@ -38,10 +36,13 @@ go.property('gravity_acceleration', 3)

go.property('sensor_length', 0.05)

go.property('is_crouching_allowed', false)

go.property('collision_standing', msg.url('#collision_standing'))
go.property('collision_crouching', msg.url('#collision_crouching'))

go.property('eyes_position_url', msg.url('eyes'))
go.property('eyes_switching', false)
go.property('eyes', msg.url('eyes'))
go.property('eyes_switch_duration', 0.2)

--
Expand Down Expand Up @@ -146,12 +147,14 @@ local function init_capsule_size(self)

-- Get the crouching height

if self.collision_crouching then
if self.is_crouching_allowed and self.collision_crouching then
msg.post(self.collision_standing, hash 'disable')

local crouching_ray_to = self.position
local crouching_ray_from = crouching_ray_to + vectors.up * size.height
physics.raycast_async(crouching_ray_from, crouching_ray_to, collision_groups, constants.collision_crouching_raycast_id)
else
self.is_crouching_allowed = false
end

return size
Expand All @@ -172,8 +175,10 @@ local function did_raycast(self, response)
self.eyes_position_crouching = self.eyes_position_standing + vectors.down * (self.capsule_size.height - self.capsule_size.height_crouching)
end
else
assert(response.fraction, 'Can\'t find the crouching collision capsule ' .. tostring(self.collision_crouching) .. ' with the ' .. tostring(self.collision_group) .. ' group')
local collision_crouching = self.collision_crouching
self.collision_crouching = nil
self.is_crouching_allowed = false
assert(response.fraction, 'Can\'t find the crouching collision capsule ' .. tostring(collision_crouching) .. ' with the ' .. tostring(self.collision_group) .. ' group')
end
end

Expand Down Expand Up @@ -371,7 +376,7 @@ local function crouch(self, is_crouching)
msg.post(self.collision_crouching, hash 'enable')

if self.eyes_position_crouching then
go.animate(self.eyes_position_url, 'position', go.PLAYBACK_ONCE_FORWARD, self.eyes_position_crouching, go.EASING_INOUTQUAD, self.eyes_switch_duration)
go.animate(self.eyes, 'position', go.PLAYBACK_ONCE_FORWARD, self.eyes_position_crouching, go.EASING_INOUTQUAD, self.eyes_switch_duration)
end

post(self.observer, hash 'walker_crouching')
Expand All @@ -382,7 +387,7 @@ local function crouch(self, is_crouching)
msg.post(self.collision_crouching, hash 'disable')

if self.eyes_position_standing then
go.animate(self.eyes_position_url, 'position', go.PLAYBACK_ONCE_FORWARD, self.eyes_position_standing, go.EASING_INOUTQUAD, self.eyes_switch_duration)
go.animate(self.eyes, 'position', go.PLAYBACK_ONCE_FORWARD, self.eyes_position_standing, go.EASING_INOUTQUAD, self.eyes_switch_duration)
end

post(self.observer, hash 'walker_standing')
Expand All @@ -395,6 +400,18 @@ end
function init(self)
msg.post('.', hash 'acquire_input_focus')

if self.observer == msg.url('#') then
self.observer = nil
end

if self.collision_crouching == msg.url('#') then
self.collision_crouching = nil
end

if self.eyes == msg.url('#') then
self.eyes = nil
end

self.bindings = {
forward = hash 'key_w',
backward = hash 'key_s',
Expand Down Expand Up @@ -453,9 +470,9 @@ function init(self)
self.collision = self.collision_standing
self.collision_group = physics.get_group(self.collision)
self.collision_mask = { hash 'default' }

if self.eyes_position_url then
self.eyes_position_standing = go.get_position(self.eyes_position_url)
if self.eyes_switching and self.eyes then
self.eyes_position_standing = go.get_position(self.eyes)
end

self.trigger_ids = { }
Expand Down

0 comments on commit 227e4e1

Please sign in to comment.