xulipa
xulipa3mo ago

Best way to change VR Avatar scale at run time

I want to change an item on the VR Avatar to get smaller and bigger based on a collision - what's the best way to do this and sync these changes to all clients?
7 Replies
kapa.ai
kapa.ai3mo ago
Hi @xulipa. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To change the scale of an item on the VR Avatar based on a collision and sync these changes to all clients, you can use the RealtimeTransform component provided by NormalVR. This component is designed to synchronize the position, rotation, and scale of a game object across all clients. Here's a general approach: 1. Attach a RealtimeTransform component to the item you want to scale. This will ensure that any changes to the item's transform will be synchronized across all clients. 2. Create a script that listens for the collision event and changes the scale of the item accordingly. You can use Unity's built-in collision detection methods, such as OnCollisionEnter(), OnCollisionStay(), or OnCollisionExit(). 3. In the collision event handler, change the scale of the item. You can do this by modifying the localScale property of the item's transform. For example, to make the item twice as large, you could use transform.localScale *= 2. 4. Since the item has a RealtimeTransform component, these changes will automatically be synchronized across all clients. Remember, the RealtimeTransform component will attempt to clear ownership automatically when the rigidbody goes to sleep in order to allow other clients to take it over on physics collisions. However, in this case, you want to retain ownership of the Player RealtimeTransform at all times. In order to do that, you'll want to set it to Maintain Ownership While Sleeping. Here's a snippet from the knowledge sources that shows how to use the RealtimeTransform component:
using UnityEngine;
using Normal.Realtime;

public class Player : MonoBehaviour {
// Camera
public Transform cameraTarget;
private float _mouseLookX;
private float _mouseLookY;

// Physics
private Vector3 _targetMovement;
private Vector3 _movement;

private bool _jumpThisFrame;
private bool _jumping;
using UnityEngine;
using Normal.Realtime;

public class Player : MonoBehaviour {
// Camera
public Transform cameraTarget;
private float _mouseLookX;
private float _mouseLookY;

// Physics
private Vector3 _targetMovement;
private Vector3 _movement;

private bool _jumpThisFrame;
private bool _jumping;
For more information, you can refer to the NormalVR documentation (https://normcore.io/documentation/guides/creating-a-player-controller#making-it-multiplayer). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@725231244169707600> kapa.ai is still learning and improving, please let us know how it did by reacting below
Mechabit
Mechabit3mo ago
the first part of the bot answer is good but I dunno what the code snippet is for
xulipa
xulipa3mo ago
Yup that's pretty random. I didn't have a RealtimeTransform onto my item because I somehow thought that the VR Avatar prefab would handle that stuff. @Normcore Assistant I shouldn't have to use a model for this since the RealtimeTransform itself will take care of syncing the scaling.
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you're correct. The RealtimeTransform component in NormalVR is designed to automatically synchronize the transform (position, rotation, and scale) of a GameObject across all clients. This means you don't need to manually sync the scale using a model. Here's how you can do it: 1. Attach a RealtimeTransform component to the item on the VR Avatar that you want to scale. Make sure the syncScale property of the RealtimeTransform is set to true. This will ensure that changes to the item's scale are synchronized across all clients. 2. In your script that handles the collision, change the localScale of the item's transform based on the collision. For example:
void OnCollisionEnter(Collision collision) {
// Change the scale based on the collision
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); // Example scale change
}
void OnCollisionEnter(Collision collision) {
// Change the scale based on the collision
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); // Example scale change
}
3. The RealtimeTransform component will automatically sync the new scale to all clients. Remember, the RealtimeTransform component works differently when a Rigidbody is present. If RealtimeTransform detects a Rigidbody component, it operates in Rigidbody mode; otherwise, it operates in Transform mode. In Rigidbody mode, RealtimeTransform introduces the concept of ownership. When an object is owned by a client, their simulation is considered the source of truth for that object. Therefore, if you want to move or scale an object, you need to request ownership of it. For more information, you can refer to the NormalVR documentation (https://normcore.io/documentation/realtime/realtimetransform#transform-mode-vs-rigidbody-mode). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@725231244169707600> kapa.ai is still learning and improving, please let us know how it did by reacting below
maxweisel
maxweisel3mo ago
if you’re using RealtimeAvatar just wire up a game object in the Root slot of RealtimeAvatarManager and scale that that’s all you need to do