There could be scenarios where you would want to understand the position of
touch within the local co-ordinate system of a 2D node. Use-cases where this is
useful
- Moving sprites within a confined location
- Virtual JoyStick implementation.
So in 3.2, you first need to convert the screen coordinates to 3D coordinates
then convert the 3D world co-ordinate to the Local Co-ordinate of the node.
This can be done as follows.
//in touch callback.
let touchLoc = event.getLocation()
let screenTouchLoc = v3(touchLoc.x, touchLoc.y, 0)
let screen2World = this.stageCamera.screenToWorld(screenTouchLoc)
let uitComponent = this.node.getComponent(UITransform)?
let touchLocInNode = uitComponent.convertToNodeSpaceAR(screen2World)
The above has helped me fix a broken virtual joystick script to get my character moving :)
So you can move around now :) #gamedev made with @CocosEngine #CocosEngine. #pixelart made with #pixelorama #samurai pic.twitter.com/xW6K9tEcwU
— VamsiKrishnaV (@vkrishna1983) July 7, 2021
Comments