Hology
Hology 文檔
Hology 文檔
  • 👋歡迎來到Hology文檔
  • Getting started
    • Hology Engine簡介
    • 第一步指南
    • 編輯器基礎指南
      • 在場景中飛行
      • 放置物件
      • 選擇物件
      • 變換
      • 分組物件
      • 複製
    • 入門遊戲模板 - 第三人稱射擊遊戲
  • Tutorials
    • 滾球 - 遊戲玩法編程
    • 角色動畫程式設計
    • Character AI behavior
  • Release
    • 遊戲發行
      • Discord Activities
      • 臉書即時遊戲
      • 上傳至 Itch.io
      • 使用 GitHub Pages
      • 發布到Steam
      • iOS 和 Android
  • Assets
    • 3D模型
      • 客製化碰撞形狀
      • 材質自訂分配
    • 材質
    • 紋理
    • 預製體
  • Gameplay
    • 演員(Actors)
      • 創建演員類
      • 演員參數
      • Actor 元件
      • Actor 的生命週期
      • 生成 Actors
      • 移動 Actors
    • 服務
      • 載入資產
    • Player input
    • Collision detection
    • Physics
      • Physics body types
      • Applying forces
      • Ray casting
    • Trigger volumes
    • Character movement
    • Pointer events
    • Animation
      • Animation system
      • Character Animation
      • Animation State Machine
    • Sound
      • Audio parameter
    • 世界
    • Navigation
  • Shaders
    • 著色器介紹
    • Creating shaders
    • 著色器參數
    • Typescript shaders
      • Types
      • Math functions
      • Attributes
      • Varying
      • Uniforms
      • Textures
      • Arrays
      • Select
      • Lighting
    • Painted materials
    • Water shader tutorial
  • Level design
    • 地形雕刻
    • 地形繪製
    • Grass
  • User Interfaces
    • 創建用戶界面UI
    • 使用 React
    • VR
  • Visual Effects
    • 視覺效果簡介
    • 視覺特效資產
  • Integrations
    • Arcweave
由 GitBook 提供支持
在本页
  • Example
  • Controlling the movement
  1. Gameplay

Character movement

For a game where you have a character that can walk, run, jump, etc, you will need some generic character movement functionality. If you want to have complete control, you could implement this yourself, but there is also a built in character movement component that you can attach to your character actor.

Example

The actor implementation below illustrates how you can attach the CharacterMovementComponent to an actor and pass in various properties to configure its behaviours. In the example we are also attaching a mesh component to give the character a visual representation. Also, the third party camera component is attached to have a camera following the character.

@Actor()
class CharacterActor extends BaseActor {
  private physicsSystem = inject(PhysicsSystem)
  private viewController = inject(ViewController)

  private mesh = attach(MeshComponent, {
    object: new Mesh(
      new CylinderBufferGeometry(.5, .5, 2),
      new MeshStandardMaterial({color: 0xffffff})
    ) 
  })
  private thirdPersonCamera = attach(ThirdPersonCameraComponent)
  public movement = attach(CharacterMovementComponent, {
    autoStepMaxHeight: 0,
    colliderHeight: 2,
    colliderRadius: .5,
    maxWalkingSlopeAngle: 70,
    maxSpeed: 3,
    maxSpeedBackwards: 3,
    maxSpeedSprint: 7,
  })
}

You likely want to tweak the parameters of the character movement component a lot to find something that works well for your game.

In this example, we make the movement component instance public because it provides an interface to control movement which we want to be accessible from a player controller.

Controlling the movement

With the above code, the character will simply fall to the ground where it is spawned and not move. To control the character you need to bind various action inputs that are exposed on the movement component.

  • directionInput - For controlling the movement of the character forward, backward, left and right

  • jumpInput - Toggle when you want the character to jump

  • sprintInput - Toggle to change the movement speed to the configured sprinting speed

  • rotationInput - Rotate the character around the y-axis to turn left and right.

The code example below illustrates how code could look inside a player controller using an injected InputService and a referenced character actor with a movement component.

const playerMove = this.character.movement.directionInput
const playerJump = this.character.movement.jumpInput
const playerSprint = this.character.movement.sprintInput

this.inputService.bindToggle(InputAction.jump, playerJump.toggle)
this.inputService.bindToggle(InputAction.sprint, playerSprint.toggle)
this.inputService.bindToggle(InputAction.moveForward, playerMove.togglePositiveY)
this.inputService.bindToggle(InputAction.moveBackward, playerMove.toggleNegativeY)
this.inputService.bindToggle(InputAction.moveLeft, playerMove.toggleNegativeX)
this.inputService.bindToggle(InputAction.moveRight, playerMove.togglePositiveX)
this.inputService.bindDelta(
  InputAction.rotate,
  this.character.movement.rotationInput.rotateY
)

Read more about how to handle player input in the Player input guide.

上一页Trigger volumes下一页Pointer events