Adjusting y position of AttachToObject
Hi,
I've been playing with AttachToObject and I want to know if it is possible to tweak this code to move the object I have attached to camera a fixed amount in y or x axis?
I thought I could use something like this:
var transform = script.getTransform();
var pos = transform.getWorldPosition();
pos.y += 1;
transform.setWorldPosition(pos);
And insert it into below but I'm having trouble getting this to work.
// -----JS CODE-----
// @input SceneObject attachTo
// @input bool smoothPosition = true
// @input float posSmoothing = 1.0
// @input bool smoothRotation = false
// @input float rotSmoothing = 1.0
if( !script.initialized ) {
script.objectTransform = script.getTransform();
script.attachToTransform = script.attachTo.getTransform();
// Initialize first frame to attach rotation and position
script.objectTransform.setWorldPosition( script.attachToTransform.getWorldPosition() );
script.objectTransform.setWorldRotation( script.attachToTransform.getWorldRotation() );
script.initialized = true;
}
if( script.smoothPosition )
{
var currentPosition = script.objectTransform.getWorldPosition();
var lerpedPosition = vec3.lerp( currentPosition, script.attachToTransform.getWorldPosition(), script.posSmoothing * getDeltaTime() );
// Set object's position to lerped position
script.objectTransform.setWorldPosition( lerpedPosition );
}
else
{
// If turned off, take position directly
script.objectTransform.setWorldPosition( script.attachToTransform.getWorldPosition() );
}
if( script.smoothRotation )
{
var currentRotation = script.objectTransform.getWorldRotation();
var lerpedRotation = quat.slerp( currentRotation, script.attachToTransform.getWorldRotation(), script.rotSmoothing * getDeltaTime() );
// Set object's rotation to slerped rotation
script.objectTransform.setWorldRotation( lerpedRotation );
}
else
{
// If turned off, take rotation directly
script.objectTransform.setWorldRotation( script.attachToTransform.getWorldRotation() );
}
Hey Tom, are you trying to animate the object? If so, I suggest creating a child object underneath your attach to that you can move independently from the attachment. Then, use the Tween system we recently added to the documentation. Specifically, the TweenTransform script. Check out the Tween system here and download the package:
https://lensstudio.snapchat.com/guides/scripting/tweening/
Your scene hierarchy should look something like this:
- AttachToObjectParent - AttachToObject.js is added to this object
-- TweenParent - TweenTransform.js is added to this object which will allow you to animate an offset
--- MyModel - The actual model you're want to attach and animate