MovementBehavior3D

Package: Libraries.Interface.Behaviors.Games · Inherits from: Libraries.Language.Object, Libraries.Interface.Behaviors.Behavior

A MovementBehavior3D moves a 3D item, such as a character, with the keyboard. Add it to an item with AddBehavior, and the item moves at once: 1. In the default mode, TURN_AND_MOVE, W and S (or the up and down arrows) move the item forward and back, and A and D (or the left and right arrows) turn it. Forward is the way the item faces, its local z axis, which is also the way Camera:Follow treats as forward. 2. In CAMERA_RELATIVE mode, the same keys move the item away from, toward, left of and right of the camera, and the item turns to face the way it moves. 3. In FLY mode, the keys work as in TURN_AND_MOVE, and E and Q (or Page Up and Page Down) move the item up and down. The space bar jumps. Every key can be changed, and each command can have more than one key. The behavior works out how to move the item from its physics settings, so the same code works for any item: 1. An item without physics is moved directly. Nothing stops it, so it passes through walls. 2. A Nonresponsive item is given a velocity. It pushes other items, and they don't push it back. 3. A Responsive item is given a level velocity, and keeps its up and down velocity, so gravity still pulls it and it can fall. It jumps only when it is standing on something. 4. An Unmovable item can't be moved. The behavior says so once, on the console. By default, the behavior keeps the item upright: it turns the item only around its y axis, and stops it from tipping over when it bumps into things. Turn this off with SetKeepUpright(false). To move the item some other way, such as from a computer-controlled player, turn the keyboard off with SetUseKeyboard(false) and set the commands with SetForward, SetTurn, SetStrafe, SetRise, and Jump.

Example Code

use Libraries.Game.Game
use Libraries.Interface.Item3D
use Libraries.Interface.Behaviors.Games.MovementBehavior3D

class Main is Game
    action Main
        UseVulkan(true)
        StartGame()
    end

    action CreateGame
        LoadScene("Scenes/Scene.qs")
        EnablePhysics3D(true)
        SetGravity3D(0, -10, 0)

        Item3D player = FindItem3DByName("Player")
        MovementBehavior3D movement
        player:AddBehavior(movement)
        GetCamera3D():Follow(player)
    end
end

Variables

  • integer CAMERA_RELATIVE: The item moves relative to the camera, and faces the way it moves.
  • integer TURN_AND_MOVE: The item turns with left and right, and moves forward and back.
  • integer FLY: As TURN_AND_MOVE, and the item can also move up and down.

Actions

AddBackKey(integer key)

This action adds another key that moves the item back.

Parameters

  • key integer: The key code.

AddDownKey(integer key)

This action adds another key that moves the item down in FLY mode.

Parameters

  • key integer: The key code.

AddForwardKey(integer key)

This action adds another key that moves the item forward.

Parameters

  • key integer: The key code.

AddJumpKey(integer key)

This action adds another key that makes the item jump.

Parameters

  • key integer: The key code.

AddLeftKey(integer key)

This action adds another key that turns the item left (or, in CAMERA_RELATIVE mode, moves it left).

Parameters

  • key integer: The key code.

AddRightKey(integer key)

This action adds another key that turns the item right (or, in CAMERA_RELATIVE mode, moves it right).

Parameters

  • key integer: The key code.

AddUpKey(integer key)

This action adds another key that moves the item up in FLY mode.

Parameters

  • key integer: The key code.

Dispose()

This action is called when the behavior is removed from an item. It stops an item with physics from moving on its own.

GetAcceleration()

This action returns how quickly the item reaches full speed.

Returns

number: The acceleration.

GetForward()

This action returns the forward command, from -1 for full speed back to 1 for full speed forward.

Returns

number: The forward command.

GetGround()

This action returns the terrain an item without physics walks on.

Returns

Libraries.Game.Graphics.Worlds.TerrainItem: The terrain, or undefined if none has been set.

GetItem()

This action returns the item this behavior moves.

Returns

Libraries.Interface.Item3D: The item, or undefined if none has been set.

GetJumpHeight()

This action returns how high the item jumps, in meters.

Returns

number: The jump height.

GetMode()

This action returns how the keys move the item: TURN_AND_MOVE, CAMERA_RELATIVE, or FLY.

Returns

integer: The mode.

GetRise()

This action returns the rise command, from -1 for moving down at full speed to 1 for moving up at full speed.

Returns

number: The rise command.

GetSpeed()

This action returns the top speed, in meters per second.

Returns

number: The speed.

GetStrafe()

This action returns the strafe command, from -1 for moving left at full speed to 1 for moving right at full speed.

Returns

number: The strafe command.

GetTurn()

This action returns the turn command, from -1 for turning left at full speed to 1 for turning right at full speed.

Returns

number: The turn command.

GetTurnSpeed()

This action returns how fast the item turns, in degrees per second.

Returns

number: The turning speed.

IsActive()

This action returns whether the behavior moves the item.

Returns

boolean: true if the behavior is active.

IsFinished()

This action returns whether the behavior is finished. A behavior added with AddBehavior keeps running until Stop is called.

Returns

boolean: true if Stop has been called, and false otherwise.

IsKeepingUpright()

This action returns whether the behavior keeps the item upright.

Returns

boolean: true if the item is kept upright.

IsOnGround()

This action returns whether the item is standing on something. An item with physics is on the ground when there is something just under it. An item without physics is on the ground when it has a ground set with SetGround.

Returns

boolean: true if the item is standing on something, and false otherwise.

IsUsingKeyboard()

This action returns whether the commands come from the keyboard.

Returns

boolean: true if the keyboard is used.

Jump()

This action asks the item to jump on the next frame. It jumps only if it can: see SetJumpHeight.

Run(Libraries.Interface.Events.BehaviorEvent event)

This action is called when the behavior is added to an item with AddBehavior. It remembers the item, so the behavior knows what to move. Most users never need to call it.

Parameters

SetAcceleration(number acceleration)

This action sets how quickly the item speeds up and slows down, as the share of full speed it can gain or lose each second. For example, 4 reaches full speed in a quarter of a second. The default is 0, which changes speed at once.

Parameters

  • acceleration number: The acceleration.

SetActive(boolean active)

This action sets whether the behavior moves the item. While it is not active, the commands are 0 and the item stops, but the behavior stays on the item and can be made active again.

Parameters

  • active boolean: true to move the item, or false to stop.

SetBackKey(integer key)

This action sets the one key that moves the item back, in place of any others. Use AddBackKey to add a key instead.

Parameters

  • key integer: The key code.

SetDownKey(integer key)

This action sets the one key that moves the item down in FLY mode, in place of any others.

Parameters

  • key integer: The key code.

SetForward(number value)

This action sets the forward command, from -1 for full speed back to 1 for full speed forward. While the keyboard is turned on, the keyboard replaces it every frame, so turn it off first with SetUseKeyboard(false).

Parameters

  • value number: The forward command.

SetForwardKey(integer key)

This action sets the one key that moves the item forward, in place of any others. Use AddForwardKey to add a key instead.

Parameters

  • key integer: The key code, such as keys:W from a KeyboardEvent.

SetGround(Libraries.Game.Graphics.Worlds.TerrainItem terrain)

This action sets a terrain for an item without physics to walk on. Each frame, the item is placed so its bottom rests on the terrain's surface, so it walks up and down hills. It has no effect on an item with physics, which stands on the terrain through collisions instead, or in FLY mode.

Parameters

SetItem(Libraries.Interface.Item3D item)

This action sets the item this behavior moves. AddBehavior sets it automatically, so most users never need to call it.

Parameters

SetJumpHeight(number height)

This action sets how high the item jumps, in meters. The default is 1. A height of 0 turns jumping off. Only a Responsive item with physics can jump, and only while it is standing on something, and gravity must be pulling down.

Parameters

  • height number: The jump height.

SetJumpKey(integer key)

This action sets the one key that makes the item jump, in place of any others.

Parameters

  • key integer: The key code.

SetKeepUpright(boolean upright)

This action sets whether the behavior keeps the item upright, turning it only around its y axis and stopping it from tipping over when it bumps into things. The default is true. When it is false, the item is turned from wherever it points, and physics can tip it over.

Parameters

  • upright boolean: true to keep the item upright.

SetLeftKey(integer key)

This action sets the one key that turns the item left (or, in CAMERA_RELATIVE mode, moves it left), in place of any others.

Parameters

  • key integer: The key code.

SetMode(integer mode)

This action sets how the keys move the item. TURN_AND_MOVE, the default, turns with left and right and moves forward and back. CAMERA_RELATIVE moves relative to the camera, and turns the item to face the way it moves. FLY works like TURN_AND_MOVE, with up and down keys as well, and no jumping.

Parameters

  • mode integer: The mode, such as movement:CAMERA_RELATIVE.

Example

use Libraries.Interface.Behaviors.Games.MovementBehavior3D

MovementBehavior3D movement
movement:SetMode(movement:CAMERA_RELATIVE)

SetRightKey(integer key)

This action sets the one key that turns the item right (or, in CAMERA_RELATIVE mode, moves it right), in place of any others.

Parameters

  • key integer: The key code.

SetRise(number value)

This action sets the rise command, from -1 for moving down at full speed to 1 for moving up at full speed. It is used only in FLY mode. While the keyboard is turned on, the keyboard replaces it every frame, so turn it off first with SetUseKeyboard(false).

Parameters

  • value number: The rise command.

SetSpeed(number speed)

This action sets the top speed, in meters per second. The default is 4, about a quick walk.

Parameters

  • speed number: The speed.

SetStrafe(number value)

This action sets the strafe command, from -1 for moving left at full speed to 1 for moving right at full speed, without turning. While the keyboard is turned on, the keyboard replaces it every frame, so turn it off first with SetUseKeyboard(false).

Parameters

  • value number: The strafe command.

SetTurn(number value)

This action sets the turn command, from -1 for turning left at full speed to 1 for turning right at full speed. It is used in TURN_AND_MOVE and FLY modes. While the keyboard is turned on, the keyboard replaces it every frame, so turn it off first with SetUseKeyboard(false).

Parameters

  • value number: The turn command.

SetTurnSpeed(number speed)

This action sets how fast the item turns, in degrees per second. The default is 120, so a full turn takes three seconds.

Parameters

  • speed number: The turning speed.

SetUpKey(integer key)

This action sets the one key that moves the item up in FLY mode, in place of any others.

Parameters

  • key integer: The key code.

SetUseKeyboard(boolean useKeyboard)

This action sets whether the commands come from the keyboard. Turn it off to move the item from code, with SetForward, SetTurn, SetStrafe, SetRise, and Jump.

Parameters

  • useKeyboard boolean: true to use the keyboard.

Stop()

This action stops the behavior. If it was added to an item with AddBehavior, the item removes it at the start of the next frame, and an item with physics stops moving on its own.

Update(number seconds)

This action is called once per frame when the behavior has been added to an item with AddBehavior, before the physics runs. It reads the keyboard, if the keyboard is turned on, and then moves the item.

Parameters

  • seconds number: The time since the last frame, in seconds.

Inherited Actions

4 actions from Libraries.Interface.Behaviors.Behavior
3 actions from Libraries.Language.Object