Skip to content

Slingshot

A slingshot that launches things into a physics game, with no code: pull its pouch back with the mouse (or a finger) and let go, and the shot in it flies off the other way, the faster the further it was pulled. Its shots are the bodies inside it, waiting their turn in a row behind it (birds, boulders, crates), and then copies of its Shot prefab, Shots of them. While it’s pulled a dotted line shows where the shot will go, and the camera follows the shot as it flies.

Part of the Physics Games module: Physics puzzlers: a Slingshot to pull back and launch shots from, and Breakable things (planks, panes, blocks, pigs) that crack and break on hard knocks; a LevelGoal that wins a level once its targets are down (with a bonus for the shots left, and one to three stars) or loses it once the shots run out; a Slingshot and a Tower to make from the Entity menu (Genres, Physics Games); and the Physics template, levels of wood, glass and stone to knock down.

With the keys or a gamepad, the arrows (A, D, W, S, the d-pad or the stick) pull the pouch about and the LaunchAction (Space, A) lets go; the CancelAction (Shift, B) lets it back. It loads its next shot by itself LoadDelay seconds after each shot comes to rest (or on its Load action), and its Launch action launches what’s loaded at its DefaultPull with nobody pulling it: a catapult a Trigger, a wire or a script fires. Its ShotsLeft output counts what it has left to launch, and OutOfShots turns on once the last has come to rest. Give it a TurnTaker and it’s pulled only on its side’s turn, each launch using one of the turn taker’s actions; untick Controllable (or Focus something else) and it ignores the mouse and keys.

Works with: Body (its shots: bodies inside it, waiting their turn); Follow (on the camera: it follows each shot as it flies); TurnTaker (its side: pulled on its side’s turn)

Takes the player’s input only while it has focus (its Controllable): its Focus action hands it the input and takes it from everything else the same player plays.

Draws itself (its entity draws at Order 0 unless it has a Sprite, whose order counts).

Each is listed as its key in the scene file and the MCP tools, then its kind of value, its default and what it does.

  • shot: text = null. A prefab it loads a copy of once the shots inside it have gone, e.g. “Bird”: up to Shots of them. Empty launches only the bodies inside it. (Names a prefab.) (Field: a name.)
  • shots: a whole number = 3. How many copies of its Shot prefab it has to launch, after the shots inside it. (Only used when shot is set.) (Field: a number.)
  • power: a number = 20. How fast a shot leaves it when pulled all the way back, in world units a second; less the less it’s pulled. (Field: a number.)
  • maxPull: a number = 2. How far its pouch can be pulled back from where it rests, in world units: the circle round its pouch in the Scene view. (A distance: the Scene view draws it as a circle around the entity, with a handle to drag.) (Field: a number.)
  • minPull: a number = 0.3. Pulled less far than this, it’s let back without launching. (Field: a number.)
  • pouch: [x, y] = [0,1.6]. Where a loaded shot rests, from its position (its foot): in the fork at its top. (A point offset from the entity, in its own units, before its scale and turned with it: with Distance Handles the Scene view puts a knob on it to drag.) (Field: a vector.)
  • defaultPull: [x, y] = [-1.8,-0.9]. How far its pouch is pulled (in the world) when its Launch action launches with nobody pulling it: (-2, -1) launches up and to the right at full power. (A point offset from the point pouch marks, in world units: with Distance Handles the Scene view puts a knob on it to drag.) (Field: a vector.)
  • autoLoad: true or false = true. Loads its next shot by itself, LoadDelay seconds after the last has come to rest (and its first as the game starts); untick it to load on its Load action. (Field: on or off.)
  • loadDelay: a number = 1.5. Seconds after its last shot has come to rest (or gone) that it loads its next (with AutoLoad). (Only used when autoLoad is on.) (Field: a number.)
  • controllable: true or false = true. Untick it to have it ignore the mouse, keys and gamepad (its actions and scripts still work): another part of the scene has the player’s input. Its Focus action ticks it and unticks it on everything else the player plays. (Field: on or off.)
  • grabRadius: a number = 1.25. How near its pouch (in world units) a press of the mouse or a finger takes hold of it. (A distance: the Scene view draws it as a circle around the entity, with a handle to drag.) (Field: a number.)
  • pullSpeed: a number = 3. How fast the keys and a gamepad pull its pouch about, in world units a second. (Field: a number.)
  • launchAction: text = “Jump”. The input action that lets go of the pouch pulled with the keys: “Jump” is Space or the gamepad’s A. (Names an input action.) (Field: a name.)
  • cancelAction: text = “Dash”. The input action that lets the pouch back without launching: “Dash” is Shift or the gamepad’s B. (Names an input action.) (Field: a name.)
  • cameraFollows: true or false = true. The camera follows each shot as it flies, and goes back to what it followed once the shot has come to rest (or the next is loaded). Needs a Follow on the camera: a slingshot made from the Entity menu gives it one following the slingshot. (Field: on or off.)
  • color: a color, “#RRGGBB” or “#RRGGBBAA” = “#8A5A34FF”. The color of its frame. (Field: a color.)
  • bandColor: a color, “#RRGGBB” or “#RRGGBBAA” = “#4A2C1EFF”. The color of its elastic bands. (Field: a color.)
  • forkWidth: a number = 0.9. How far apart the two prongs of its fork are, in world units. (Field: a number.)
  • preview: a number = 1. Seconds of a shot’s flight the dotted line shows while it’s pulled; 0 shows none. (Field: a number.)
  • previewColor: a color, “#RRGGBB” or “#RRGGBBAA” = “#FFFFFFB2”. The color of the dots showing where a shot will go. (Only used when preview is set.) (Field: a color.)
  • launchSound: text = null. A sound played as a shot is launched, e.g. “Sounds/twang.wav”. (A sound in Assets, e.g. “Sounds/hit.wav”.) (Field: a sound.)
  • stretchSound: text = null. A sound played as its pouch is first pulled back. (A sound in Assets, e.g. “Sounds/hit.wav”.) (Field: a sound.)
  • launchMessage: text = null. A message sent with Game.Send as a shot is launched, with the shot as its data, e.g. “Launched”. (Names a message.) (Field: a name.)
  • outOfShotsMessage: text = null. A message sent once it has no shots left and its last has come to rest, e.g. “OutOfShots”: time to lose. (Names a message.) (Field: a name.)
  • pull: [x, y] = [0,0]. How far its pouch is pulled from where it rests, in the world, right now. Set by the game as it’s pulled. (Field: a vector.)
  • loaded: true or false = false. True while a shot sits in its pouch: its first shot inside it. Set by the game. (Field: on or off.)
  • made: a whole number = 0. How many copies of its Shot prefab it has made so far. Set by the game. (Field: a number.)
  • loadIn: a number = 0. Seconds until it loads its next shot by itself; 0 when it isn’t waiting to. Set by the game. (Field: a number.)
  • flying: text = null. The shot it last launched, by its entity’s id, while it’s flying: what the camera follows. Set by the game. (Plain text: the shot in the air: the game sets it.) (Field: text.)
  • flyingStill: a number = 0. Seconds the shot it last launched has been all but still. Set by the game. (Field: a number.)
  • flyingFor: a number = 0. Seconds the shot it last launched has been flying. Set by the game. (Field: a number.)
  • outOfShots: true or false = false. True once it has no shots left (none inside it, none of its prefab) and the last it launched has come to rest or gone: wire it to a panel that says the level’s lost. Set by the game. (Field: on or off.)

What anything can make it do with no code: a Trigger’s do when its message arrives, a Contraptions Wire’s input as its signal comes on, a script’s Game.Do(entity, “Name”).

  • Load. Loads its next shot into its pouch now, if it has none loaded and has one left.
  • Launch. Launches the shot in its pouch as it’s pulled, or at its Default Pull with nobody pulling it (a catapult a trigger fires).
  • Focus. Gives its entity the player’s input: whatever there and inside it has focus, and everything else the same player plays loses it (a platformer’s player stops on the arrow keys while a board takes them).

What anything can read with no code: a Counter’s, Bar’s or Text’s output, a Contraptions Wire’s output as its signal; Text shows names too, the rest read numbers, true as 1.

  • OutOfShots (true or false). True once it has no shots left (none inside it, none of its prefab) and the last it launched has come to rest or gone: wire it to a panel that says the level’s lost. Set by the game.
  • ShotsLeft (a whole number). How many shots it has left to launch: those inside it (the loaded one too) and the copies of its prefab still to come.
  • Stretch (a number). How far it’s pulled, from 0 (resting) to 1 (all the way back).

Not saved with the scene; scripts read them, and entity_get shows them as live while the game plays.

  • shotsLeft: a whole number. How many shots it has left to launch: those inside it (the loaded one too) and the copies of its prefab still to come.
  • stretch: a number. How far it’s pulled, from 0 (resting) to 1 (all the way back).
  • loadedShot: an entity: its name, id or path. The shot in its pouch while it’s Loaded, or null.
  • pouchInWorld: [x, y]. Where a loaded shot rests in the world: its Pouch.

Scripts use it as Slingshot: its methods, events and values that aren’t saved with the scene.