Move a object by user only in direction

I had a similar problem here Drag object along paths

You can use the Line3 to make a line from corner to corner of each wall and then get the closestPointToPoint and set your obj to it. Make sure to use clamping = true

this.line = new THREE.Line3(startVec3, endVec3)

public move(moveTo: THREE.Vector3) {

    this.line.closestPointToPoint(moveTo, true, moveTo)

    // To limit movement only in x or z axes
    this.obj.position.x = moveTo.x
    this.obj.position.z = moveTo.z
}
1 Like