rbx-character-controller
v0.0.15
Published
A character controller for roblox-ts.
Downloads
7
Readme
rbx-character-controller
Example
import { CharacterController, State, Animation } from "rbx-character-controller"
import { UserInputService, Players } from "rbx-services"
const player = Players.LocalPlayer as Player
const states = new Array<typeof State>()
class None extends State {
static id = "None"
static init(characterController: CharacterController) {
}
static start(characterController: CharacterController) {
characterController.setVelocity(new Vector3())
}
static stop(characterController: CharacterController) {
}
}
states.push(None)
class Walk extends State {
static id = "Walk"
static animation: Animation
static init(characterController: CharacterController) {
this.animation = characterController.loadAnimation(180426354)
characterController.moveInput((moveDirection: Vector3) => {
characterController.move(moveDirection)
if (moveDirection !== new Vector3(0, 0, 0)) {
if (characterController.isGrounded()) {
if (!this.animation.isPlaying()) {
this.animation.play()
}
} else {
this.animation.stop()
}
} else {
this.animation.stop()
}
})
}
static start() {
}
static stop() {
}
}
states.push(Walk)
class Jump extends State {
static id = "Jump"
static animation: Animation
static init(characterController: CharacterController) {
this.animation = characterController.loadAnimation(125750702)
characterController.jumpInput(() => {
if (characterController.isGrounded()) {
characterController.setState("Jump")
}
})
}
static start(characterController: CharacterController) {
const connection = characterController.landed(() => {
connection.Disconnect()
if (characterController.getState() === "Jump") {
characterController.setState("None")
}
})
characterController.bounce(50)
Jump.animation.play()
}
static stop(characterController: CharacterController) {
}
}
states.push(Jump)
player.CharacterAdded.Connect(character => {
character.PrimaryPart = character.WaitForChild("HumanoidRootPart")
const characterController = new CharacterController(character)
characterController.addStates(states)
characterController.setState("None")
})
Documentation
CharacterController
constructor(character: Model)
// Creates a new CharacterController. Expressed as new CharacterController(character).
getVelocity(): Vector3
// Gets the character velocity.
setVelocity(velocity: Vector3)
// Sets the character velocity.
getMobile(): boolean
// Returns true if the character is mobile.
setMobile(mobile: boolean)
// Sets the mobile state.
bounce(height: number)
// Bounces the character to the specified height
move(direction: Vector3)
// Moves the character in the specified direction.
isGrounded(): boolean
// Returns true if the character is grounded.
isMoving(): boolean
// Returns true if the character is moving
humanoidStateChanged(event: Function): RBXScriptConnection
// Runs event when the humanoid state changes.
landed(event: Function): RBXScriptConnection
// Runs when the character lands.
getPrimaryPart(): BasePart
// Gets the primary part of the character.
getHumanoid(): Humanoid
// Gets the humanoid of the character.
jumpInput(event: Function): RBXScriptConnection
// Runs event when a jump input is pressed (Space, A, Mobile jump button, etc.).
moveInput(event: Function): RBXScriptConnection
// Runs event every frame and passes moveDirection as a param.
crouchInput(event: Function): RBXScriptConnection
// Runs event when a crouch input is pressed (Shift, LT/L2, etc.).
addState(state: typeof State)
// Adds a state to the character.
addStates(states: Array<typeof State>)
// Adds states to the character.
setState(stateName: string)
// Sets the character state.
getState(): State
// Gets the character state.
loadAnimation(animationId: number): Animation
// Loads the specified animation
getAnimatable(): boolean
// Gets the animatable state.
setAnimatable(animatable: boolean)
// Sets the animatable state.
character: Model
// The character model.
State
static id: string
// The id of the state.
static init()
// Called when the state initializes.
static start()
// Called when the state starts.
static stop()
// Called when the state stops.
Animation
play()
// Plays the animation.
stop()
// Stops the animation.
isPlaying()
// Returns true if the animation is playing.