Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to change the scale of an object while it is slotted in an XRSocketInteractor?

Discussion in 'XR Interaction Toolkit and Input' started by Honeyboi, Jul 8, 2023.

  1. Honeyboi

    Honeyboi

    Joined:
    Apr 7, 2017
    Posts:
    3
    Hello! In my project, players are able to remove objects from an XRSocket, scale them with a two-handed grab, and then return them to the socket. However, I would like to make it so those objects return to their initial scale when they are put in the socket and, right now, it seems like the Transform values are locked the moment the object is put in the socket.

    Code
    Code (CSharp):
    1. public class GrabbableArtifact : MonoBehaviour
    2. {
    3.     //Return to pedestal varibles
    4.     public XRSocketInteractor initialPedestal;
    5.     public Vector3 initialScale;
    6.     public bool isScaling = false;
    7.     public Vector3 scaleingValue = new Vector3(.01f, .01f, .01f);
    8.  
    9.     void Start()
    10.     {
    11.         RoundScale();
    12.         initialScale = this.transform.localScale;
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if(isScaling)
    18.         {
    19.             if (this.transform.localScale.x > initialScale.x)
    20.             {
    21.                 this.transform.localScale -= scaleingValue;
    22.             }
    23.  
    24.             if (this.transform.localScale.x < initialScale.x)
    25.             {
    26.                 this.transform.localScale += scaleingValue;
    27.             }
    28.  
    29.             if (this.transform.localScale.x == initialScale.x)
    30.             {
    31.                 isScaling = false;
    32.             }
    33.         }
    34.     }
    35.  
    36.     public void OnCollisionEnter(Collision collision)
    37.     {
    38.         if (collision.gameObject.CompareTag("Floor"))
    39.         {
    40.             ReturnToPedestal(true);
    41.         }
    42.     }
    43.  
    44.     public void ReturnToPedestal(bool teleport)
    45.     {
    46.         if(teleport)
    47.         {
    48.             XRSocketInteractor targetPedestal = initialPedestal;
    49.             this.transform.position = targetPedestal.attachTransform.transform.position;
    50.         }
    51.  
    52.         isScaling = true;
    53.     }
    54.  
    55.     public void RoundScale()
    56.     {
    57.         float roundedScale = Mathf.Round(this.transform.localScale.x * 100) / 100;
    58.         this.transform.localScale = new Vector3(roundedScale, roundedScale, roundedScale);
    59.     }
    60. }
    The ReturnToPedestal(), which starts the scaling, function is called when the object is dropped (Last Select Exit Interactable Event), and it works just fine if I drop the object, but doesn't actually do anything if I "drop" the object into the socket. The bool isScaling toggles to true but the update function does no change the scale.

    Can I go into the XRSocketInteractor script and make it so the transform.localScale is not locked when the object is placed in the socket? Is the a smarter way to do this?

    Thank you :)
     
  2. ericprovencher

    ericprovencher

    Unity Technologies

    Joined:
    Dec 17, 2020
    Posts:
    144
    Hey I have some work I’ve been doing to resolve exactly this. You’ll have to wait for a future release to handle it properly. In the interim what I can recommend is to have your renderer on a child game object and modify of the scale of that.
     
  3. Honeyboi

    Honeyboi

    Joined:
    Apr 7, 2017
    Posts:
    3
    Sounds like an excellent work-around, I'll implement that right now!

    Out of curiosity, do you know what lines in the XRSocketInteractor script "freeze" the transform values of the socketed object? I was having trouble looking through that.
     
    ericprovencher likes this.
  4. ericprovencher

    ericprovencher

    Unity Technologies

    Joined:
    Dec 17, 2020
    Posts:
    144
    The socket interactor just uses whatever transformer is assigned to the object being socketed. You can actually write a custom transformer that modifies the scale as you see fit, that the socket interactor will use.
     
  5. Honeyboi

    Honeyboi

    Joined:
    Apr 7, 2017
    Posts:
    3
    Ah so is the scale being locked by the XRGeneralGrabTransformer that is instantiated at runtime? Right now, I can't change the scale of the Transform while it is socketed, even with the editor.
     
  6. ericprovencher

    ericprovencher

    Unity Technologies

    Joined:
    Dec 17, 2020
    Posts:
    144
    The transformer can update the scale but it can also just pass it through. You can stack transformers though, and have one that applies after that just modifies the scale.

    The grab interactable does hold the scale hostage though. The update I’m working on will let you turn off any control over the scale from the grab interactable as well.