Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CharactorController move not effect with transform changed in update func #2322

Merged
merged 10 commits into from
Aug 13, 2024
14 changes: 11 additions & 3 deletions packages/core/src/physics/CharacterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export class CharacterController extends Collider {
this._setUpDirection = this._setUpDirection.bind(this);
//@ts-ignore
this._upDirection._onValueChanged = this._setUpDirection;

// sync world position to physical space
this._onUpdate();
}

/**
Expand All @@ -90,7 +93,9 @@ export class CharacterController extends Collider {
* @return flags - The ControllerCollisionFlag
*/
move(disp: Vector3, minDist: number, elapsedTime: number): number {
return (<ICharacterController>this._nativeCollider).move(disp, minDist, elapsedTime);
const flags = (<ICharacterController>this._nativeCollider).move(disp, minDist, elapsedTime);
this._syncWorldPositionFromPhysicalSpace();
return flags;
}

/**
Expand Down Expand Up @@ -135,8 +140,7 @@ export class CharacterController extends Collider {
* @internal
*/
override _onLateUpdate() {
const position = this.entity.transform.worldPosition;
(<ICharacterController>this._nativeCollider).getWorldPosition(position);
this._syncWorldPositionFromPhysicalSpace();
this._updateFlag.flag = false;
}

Expand Down Expand Up @@ -164,6 +168,10 @@ export class CharacterController extends Collider {
}
}

private _syncWorldPositionFromPhysicalSpace(): void {
(<ICharacterController>this._nativeCollider).getWorldPosition(this.entity.transform.worldPosition);
}

private _setUpDirection(): void {
(<ICharacterController>this._nativeCollider).setUpDirection(this._upDirection);
}
Expand Down
Loading