Add Scaling and Rotation Gestures for Spawned Objects

  1. Pinch (two-finger gesture) → scale the last placed object

  2. **Drag (single-finger gesture) → rotate the last placed object

    I tried this script But not working

    **
    // — PLACE OBJECT —

      .listen(eid, ecs.input.SCREEN_TOUCH_START, (e) => {
    
        const {prefabToSpawn, minScale, maxScale, hasPlaced} = schemaAttribute.get(eid)
    
    
    
        if (prefabToSpawn && hasPlaced === 0) {
    
          const {worldPosition} = e.data as any
    
          const newInstance = world.createEntity(prefabToSpawn)
    
          const randomScale = Math.random() \* (maxScale - minScale) + minScale
    
    
    
          ecs.Position.set(world, newInstance, worldPosition)
    
    
    
          ecs.ScaleAnimation.set(world, newInstance, {
    
            fromX: 0,
    
            fromY: 0,
    
            fromZ: 0,
    
            toX: randomScale,
    
            toY: randomScale,
    
            toZ: randomScale,
    
            duration: 400,
    
            loop: false,
    
            easeOut: true,
    
            easingFunction: 'Quadratic',
    
          })
    
    
    
          schemaAttribute.set(eid, {
    
            hasPlaced: 1,
    
            lastPlaced: newInstance,
    
          })
    
    
    
          toCooldown.trigger()
    
        }
    
      })
    
    
    
      ***// --- ONE-FINGER DRAG FOR ROTATION ---***
    
      **.listen(eid, ecs.input.SCREEN_TOUCH_MOVE, (e) => {**
    
        **const {lastPlaced} = schemaAttribute.get(eid)**
    
        **if (!lastPlaced) return**
    
    
    
        **const dragData = e.data as any**
    
        **const dragDeltaX = dragData.deltaX**
    
        **if (dragDeltaX === undefined) return**
    
    
    
        **const rotationSpeed = 0.005**
    
        **const currentRotation = ecs.Quaternion.get(world, lastPlaced)**
    
    
    
        **if (currentRotation) {**
    
          **ecs.Quaternion.set(world, lastPlaced, {**
    
            **x: currentRotation.x,**
    
            **y: currentRotation.y + dragDeltaX \* rotationSpeed,**
    
            **z: currentRotation.z,**
    
            **w: currentRotation.w,**
    
          **})**
    
        **}**
    
      **})**
    
    
    
      ***// --- TWO-FINGER PINCH FOR SCALE ---***
    
      **.listen(eid, ecs.input.GESTURE_MOVE, (e) => {**
    
        **const {lastPlaced, minScale, maxScale} = schemaAttribute.get(eid)**
    
        **if (!lastPlaced) return**
    
    
    
        **const gestureData = e.data as any**
    
        **if (!gestureData.scaleDelta) return**
    
    
    
        **const currentScale = ecs.Scale.get(world, lastPlaced)**
    
        **if (!currentScale) return**
    
    
    
        **const pinchDelta = gestureData.scaleDelta**
    
        **const newScale = \[**
    
          **Math.min(Math.max(currentScale\[0\] \* pinchDelta, minScale), maxScale),**
    
          **Math.min(Math.max(currentScale\[1\] \* pinchDelta, minScale), maxScale),**
    
          **Math.min(Math.max(currentScale\[2\] \* pinchDelta, minScale), maxScale),**
    
        **\]**
    
    
    
        **ecs.Scale.set(world, lastPlaced, {**
    
          **x: newScale\[0\],**
    
          **y: newScale\[1\],**
    
          **z: newScale\[2\],**
    
        **})**
    
      **})**
    

Hi, welcome to the forum!

Can you provide a bit more information, What’s not working with the script?

  1. Pinch (two-finger gesture) → scale the last placed object

  2. **Drag (single-finger gesture) → rotate the last placed object

    I tried this script But not working

    **
    // — PLACE OBJECT —

      .listen(eid, ecs.input.SCREEN_TOUCH_START, (e) => {
    
        const {prefabToSpawn, minScale, maxScale, hasPlaced} = schemaAttribute.get(eid)
    
    
    
        if (prefabToSpawn && hasPlaced === 0) {
    
          const {worldPosition} = e.data as any
    
          const newInstance = world.createEntity(prefabToSpawn)
    
          const randomScale = Math.random() \* (maxScale - minScale) + minScale
    
    
    
          ecs.Position.set(world, newInstance, worldPosition)
    
    
    
          ecs.ScaleAnimation.set(world, newInstance, {
    
            fromX: 0,
    
            fromY: 0,
    
            fromZ: 0,
    
            toX: randomScale,
    
            toY: randomScale,
    
            toZ: randomScale,
    
            duration: 400,
    
            loop: false,
    
            easeOut: true,
    
            easingFunction: 'Quadratic',
    
          })
    
    
    
          schemaAttribute.set(eid, {
    
            hasPlaced: 1,
    
            lastPlaced: newInstance,
    
          })
    
    
    
          toCooldown.trigger()
    
        }
    
      })
    
    
    
      ***// --- ONE-FINGER DRAG FOR ROTATION ---***
    
      **.listen(eid, ecs.input.SCREEN_TOUCH_MOVE, (e) => {**
    
        **const {lastPlaced} = schemaAttribute.get(eid)**
    
        **if (!lastPlaced) return**
    
    
    
        **const dragData = e.data as any**
    
        **const dragDeltaX = dragData.deltaX**
    
        **if (dragDeltaX === undefined) return**
    
    
    
        **const rotationSpeed = 0.005**
    
        **const currentRotation = ecs.Quaternion.get(world, lastPlaced)**
    
    
    
        **if (currentRotation) {**
    
          **ecs.Quaternion.set(world, lastPlaced, {**
    
            **x: currentRotation.x,**
    
            **y: currentRotation.y + dragDeltaX \* rotationSpeed,**
    
            **z: currentRotation.z,**
    
            **w: currentRotation.w,**
    
          **})**
    
        **}**
    
      **})**
    
    
    
      ***// --- TWO-FINGER PINCH FOR SCALE ---***
    
      **.listen(eid, ecs.input.GESTURE_MOVE, (e) => {**
    
        **const {lastPlaced, minScale, maxScale} = schemaAttribute.get(eid)**
    
        **if (!lastPlaced) return**
    
    
    
        **const gestureData = e.data as any**
    
        **if (!gestureData.scaleDelta) return**
    
    
    
        **const currentScale = ecs.Scale.get(world, lastPlaced)**
    
        **if (!currentScale) return**
    
    
    
        **const pinchDelta = gestureData.scaleDelta**
    
        **const newScale = \[**
    
          **Math.min(Math.max(currentScale\[0\] \* pinchDelta, minScale), maxScale),**
    
          **Math.min(Math.max(currentScale\[1\] \* pinchDelta, minScale), maxScale),**
    
          **Math.min(Math.max(currentScale\[2\] \* pinchDelta, minScale), maxScale),**
    
        **\]**
    
    
    
        **ecs.Scale.set(world, lastPlaced, {**
    
          **x: newScale\[0\],**
    
          **y: newScale\[1\],**
    
          **z: newScale\[2\],**
    
        **})**
    
      **})
    
    1. i am testing on mobile.

    2. When you tap the screen, a new instance (object) is created successfully.

    3. After that, when you pinch (zoom in/out) or rotate, nothing happens.

      One-Finger Drag for Rotation
      .listen(eid, ecs.input.SCREEN_TOUCH_MOVE, (e) => { … })
      // — ONE-FINGER DRAG FOR ROTATION —

        .listen(eid, ecs.input.SCREEN_TOUCH_MOVE, (e) => {
      
          const {lastPlaced} = schemaAttribute.get(eid)
      
      
      
          console.log('Scale')
      
      
      
          if (!lastPlaced) return
      
          const dragData = e.data as any
      
          console.log('Drag Data:', dragData)
      
      
      
          *// Handle null, undefined, or missing properties*
      
          const dragDeltaX = dragData.deltaX ?? dragData.movementX ?? 0
      
          if (dragDeltaX === 0) return
      
      
      
          console.log('drag detected')
      
      
      
          const rotationSpeed = 0.005
      
          const currentRotation = ecs.Quaternion.get(world, lastPlaced)
      
      
      
          if (currentRotation) {
      
            ecs.Quaternion.set(world, lastPlaced, {
      
              x: currentRotation.x,
      
              y: currentRotation.y + dragDeltaX \* rotationSpeed,
      
              z: currentRotation.z,
      
              w: currentRotation.w,
      
            })
      
          }
      
        })
      

      Note : if (dragDeltaX === 0) reurn
      dragDeltaX is zero code return
      cant go further line

      Two-Finger Pinch for Scale
      .listen(eid, ecs.input.GESTURE_MOVE, (e) => { … })
      is it correct??
      i used this condition for my script.

I can’t see anything wrong with the script, could you share the project with support so I can take a look?

Thanku you for reply
i share my project

Goal

After spawning an object in your scene, =want to:

  1. Rotate the object by dragging

  2. Pinch to zoom (scale up or down) using two fingers.

Looking at the script it looks like you’re listening for the events on the eid of the entity that has the script but I would probably listen to the event for each object you spawn with addEventListener right after it’s spawned. That way you’re getting the touched event etc per object instead of once for the most recent object. This would also let you do the gesture directly on the object.