1

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.

1 Answer 1

1

I've tested this script in a simple scene and I don't see the jitter you're talking about. The code is simple enough and should behave correctly. Most likely, something is interfering with your object and tries to resets its scale (maybe the XR Grab interactable).

You could try and use a different function to update the scale, such as FixedUpdate or LateUpdate and see if the behaviour changes.

Side note:

  1. You should use OnEnable instead of Start.
  2. Remove both comments on your methods.
  3. Clean up your using.
  4. Remove irrelevant variables
  5. Refactor
using UnityEngine;

public class UpdateScaleObject : MonoBehaviour
{
    public GameObject target;

    private Vector3 lastPos;
    private Vector3 currPos;

    private void OnEnable()
    {
        lastPos = target.transform.localPosition;
    }

    private void Update()
    {
        currPos = target.transform.localPosition;

        Debug.Log("Local Scale: " + transform.localScale);
        Debug.Log("+");
        Debug.Log("Current Position R: " + currPos);
        Debug.Log("-");
        Debug.Log("Last Position R: " + lastPos);
        Debug.Log("=");

        transform.localScale += currPos - lastPos;

        Debug.Log("Local Scale: " + transform.localScale);
        Debug.Log("~~~~~~~~~~~~");

        lastPos = currPos;
    }
}

If you do, you'll see that the code is pretty simple and that the code can be tested without XR

1
  • Thank you for your reply! What method did you use other than the grab interactable?
    – A J
    Commented Mar 23, 2023 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.