I'm trying to write a function that can scale a 3D shape in any direction. This function is attached to an object and will be enabled when the object is grabbed by the player. After the script is enabled the object will be scaled relative to the movement of the hand. The function below changes the scale, but after any update the scale changes back to it's pre-grabbed state. It almost "jitters" between the original state and the changed new scaled shape as it's grabbed. It is hard to describe without a video. Please lmk if any clarification is needed.
Shape we're trying to scale Debug logs while trying to scale object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR.Interaction.Toolkit;
public class UpdateScaleObject : MonoBehaviour
{
public GameObject object2Scale;
public GameObject leftHand;
public GameObject rightHand;
private Vector3 lastPosL;
private Vector3 currPosL;
private Vector3 lastPosR;
private Vector3 currPosR;
public InputActionProperty leftGrab;
public InputActionProperty rightGrab;
public InputActionProperty leftScale;
public InputActionProperty rightScale;
// Start is called before the first frame update
void Start()
{
currPosL = leftHand.transform.localPosition;
currPosR = rightHand.transform.localPosition;
}
// Update is called once per frame
void Update()
{
lastPosL = currPosL;
currPosL = leftHand.transform.localPosition;
lastPosR = currPosR;
currPosR = rightHand.transform.localPosition;
Debug.Log("Local Scale: "+ transform.localScale);
Debug.Log("+");
Debug.Log("Current Position R: "+ currPosR);
Debug.Log("-");
Debug.Log("Last Position R: "+ lastPosR);
Debug.Log("=");
transform.localScale += (currPosR - lastPosR);
Debug.Log("Local Scale: "+ transform.localScale);
Debug.Log("~~~~~~~~~~~~");
}
}
is script is based off a working script that was part of the XR origin. That script had one single object2scale hard coded to only change the scale of that single pre-determined object. This script is meant to be a component of an object so that the player can select any object they want to scale in the environment by grabbing it. For whatever reason making this script a component of an object seems to be causing the bug.