How to control continuous movement by long pressing on the GameController


struct GameSystem: System {
    
    static let rootQuery = EntityQuery(where: .has(GameMoveComponent.self) )
    
    init(scene: RealityKit.Scene) { }
    
    func update(context: SceneUpdateContext) {
        
        
        let root = context.scene.performQuery(Self.rootQuery)
        
        for entity in root{
            let game = entity.components[GameMoveComponent.self]!
            
            
            if let xMove = game.game.gc?.extendedGamepad?.dpad.xAxis.value ,
               let yMove = game.game.gc?.extendedGamepad?.dpad.yAxis.value {
                print("x:\(xMove),y:\(yMove)")
                let x = entity.transform.translation.x + xMove * 0.01
                let y = entity.transform.translation.z - yMove * 0.01
                entity.transform.translation = [x , entity.transform.translation.y , y]
            }
            
        }
    }
    
}

I want to use the game controller's direction keys to control the continuous movement of Entity in visionOS. When I added a query for handle button presses in the ECS System, I found that the update interface was not called at a frequency of 30 frames per second. Instead, it executes once when I press or release the key.

Is this what is the reason?

I want to keep moving by holding down the controller button, is there a better solution? I hope this moving process will be smooth and not stuck.

@xuhengfei,

Based off the limited code snippet you provided I'm not sure what is occurring. Is it possible that you are losing reference to either the gc or extendedGamepad?

The System's update(context:) method should be firing every frame as documented in RealityKit Systems. You will want to use context.deltaTime to determine how far you want the entity to move.

The Happy Beam sample code contains a robust example of using controllers in visionOS. Additionally, my colleague provided an example on how to accomplish this in this forum post.

Hope this help!

Michael

How to control continuous movement by long pressing on the GameController
 
 
Q