I see, it seems there is an issue accessing the getComponent method directly from the Object3D. To resolve this issue, you can call the getComponent method on a GameObject instance rather than on the Object3D. Here’s the corrected code:
import { Behaviour, GameObject, Object3D, serializable } from "@needle-tools/engine";
export class MovementManager extends Behaviour {
@serializable(Behaviour)
public boardAreaList?: BoardArea[];
@serializable(Behaviour)
public boardArea2x2Matrix?: BoardArea[][] | undefined;
@serializable(GameObject)
public boardPrefab?: GameObject;
InitBoardArea() {
this.boardPrefab?.children.forEach(child => {
child.children.forEach(boardAreaTransform => {
const gameObject = boardAreaTransform.gameObject;
const boardAreaComponent = gameObject.getComponent(BoardArea);
if (boardAreaComponent) {
this.boardAreaList?.push(boardAreaComponent);
}
});
});
}
}
In this code, we access the gameObject property from the boardAreaTransform to get the corresponding GameObject instance, and then call the getComponent method on that GameObject instance to retrieve the BoardArea component.
0 messages leftGPT 3.5 thread_bQ3fzLLgKb9EY2teLyXwmrHZ Thanks for your feedback!
To convert world space coordinates to local space in Needle Engine, you can use the worldToLocal method provided by the Object3D class. If you have a Vector3 representing a position in world space, you can apply this method to a particular object to get the equivalent local space coordinates.
Here’s an example of how to use it:
import { Vector3, Object3D } from "three";
// Assuming 'worldPosition' is a Vector3 representing a position in world space
// and 'object' is an instance of Object3D (e.g., your gameObject).
// Convert worldPosition to object's local space
const worldPosition = new Vector3(/* x, y, z values in world space */);
const localPosition = object.worldToLocal(worldPosition.clone());
The clone method is used to avoid modifying the original worldPosition. Now, localPosition contains the coordinates relative to the object’s local space. Remember that object can be any instance of Object3D, including this.gameObject in the context of a Behaviour component.
4 messages leftGPT 4 thread_bQ3fzLLgKb9EY2teLyXwmrHZ Thanks for your feedback!