Hi there
In Studio Iβm trying to create a custom component that triggers an animation on a click/touch event
I have 2 scripts that work individually. A script that console logs when a Plane is clicked, and another script that Animates an entity position.
I want to combine or create a script that triggers the animation based on a click, instead of the animation starting immediately as is the case now.
Below is the code for those seperate scripts
Is there someone who could help guide me in the right direction?
Click script
import * as ecs from '@8thwall/ecs'
// Register the UIController component
ecs.registerComponent({
name: 'LogClickController',
schema: {
planeEntity: ecs.eid, // Entity ID for the plane in the scene
},
stateMachine: ({world, eid, schemaAttribute}) => {
// Function to log a message when the plane is clicked
const logMessage = () => {
console.log('Plane clicked! Logging a message.') // Log message
}
// Define the state and handle click on the plane
ecs.defineState('start').initial()
.onEvent(ecs.input.SCREEN_TOUCH_START, 'logMessage', {
target: schemaAttribute.cursor(eid).planeEntity,
})
.onEnter(() => {
console.log('Event listener added to plane.') // Log message when event listener is set
world.events.addListener(schemaAttribute.cursor(eid).planeEntity,
ecs.input.SCREEN_TOUCH_START, logMessage)
})
.onExit(() => {
// Remove event listener when exiting the state
console.log('Event listener removed from plane.') // Log message when event listener is removed
world.events.removeListener(schemaAttribute.cursor(eid).planeEntity,
ecs.input.SCREEN_TOUCH_START, logMessage)
})
},
})
Animation script
import * as ecs from '@8thwall/ecs'
ecs.registerComponent({
name: 'PositionAnimation',
schema: {
targetX: ecs.f32, // x-position target for animation
targetY: ecs.f32, // y-position target for animation
targetZ: ecs.f32, // z-position target for animation
duration: ecs.f32, // Duration of the animation
},
data: {
startX: ecs.f32,
startY: ecs.f32,
startZ: ecs.f32,
elapsedTime: ecs.f32,
isAnimating: ecs.boolean,
},
// Function to start the animation
add: (world, component) => {
const {data, schema} = component
data.startX = ecs.Position.get(world, component.eid).x
data.startY = ecs.Position.get(world, component.eid).y
data.startZ = ecs.Position.get(world, component.eid).z
data.elapsedTime = 0
data.isAnimating = true
},
tick: (world, component) => {
const {data, schema} = component
if (data.isAnimating) {
data.elapsedTime += world.time.delta // Increase elapsed time by delta time
const progress = Math.min(data.elapsedTime / schema.duration, 1) // Calculate progress (0 to 1)
const currentX = data.startX + (schema.targetX - data.startX) * progress
const currentY = data.startY + (schema.targetY - data.startY) * progress
const currentZ = data.startZ + (schema.targetZ - data.startZ) * progress
// Update position based on the animation progress
ecs.Position.set(world, component.eid, {x: currentX, y: currentY, z: currentZ})
// If animation is complete, stop it
if (progress === 1) {
data.isAnimating = false
}
}
},
})