MasterOX 水
MasterOX 水2mo ago

physics question

if i have a script that applies a force to an object every frame, will it apply the force twice if 2 players are in the lobby?
8 Replies
kapa.ai
kapa.ai2mo ago
Hi @MasterOX 水. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, if you have a script that applies a force to an object every frame and it's attached to an avatar, it will apply the force twice if two players are in the lobby. This is because scripts that live on the avatar prefab are instantiated for each avatar, not just the local player. This means that your code will execute on local and remote avatars, once for every player in the scene. If you want the logic to only apply to a single player, you can use the RealtimeView's isOwnedLocallyInHierarchy property. This property allows you to check if the current instance of the script is owned by the local player. If it is, then you can execute your force application code. If it's not, then you can skip applying the force. Here's an example of how you can use isOwnedLocallyInHierarchy:
private void Update() {
// Call LocalUpdate() only if this instance is owned by the local client
if (_realtimeView.isOwnedLocallyInHierarchy)
LocalUpdate();
}
private void Update() {
// Call LocalUpdate() only if this instance is owned by the local client
if (_realtimeView.isOwnedLocallyInHierarchy)
LocalUpdate();
}
In this example, LocalUpdate() would contain the code that applies the force to the object. For more information, you can refer to the Player Controller guide (https://normcore.io/documentation/guides/creating-a-player-controller#making-it-multiplayer) on the Normcore documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
MasterOX 水
MasterOX 水2mo ago
@Normcore Assistant instead of _realtimeView.isOwnedLocallyInHierarchy i can do if(_realtimeView.clientId==0) right?
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- No, using _realtimeView.clientId == 0 is not the same as _realtimeView.isOwnedLocallyInHierarchy. The _realtimeView.clientId == 0 check would only be true if the client ID of the component owner is 0. This is not necessarily the local client. Client IDs are assigned by the server and can be any integer, not just 0. On the other hand, _realtimeView.isOwnedLocallyInHierarchy is a property that specifically checks if the component and all of its parent views are owned by the local client. If you want to ensure that a piece of code only runs for the local client, you should use _realtimeView.isOwnedLocallyInHierarchy. For more information, you can refer to the RealtimeView documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.RealtimeView) and the IRealtimeComponent documentation (https://normcore.io/documentation/reference/classes/Normal.Realtime.IRealtimeComponent). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@758040514678489279> kapa.ai is still learning and improving, please let us know how it did by reacting below
MasterOX 水
MasterOX 水2mo ago
is this actually true?
Mechabit
Mechabit2mo ago
you're not guaranteed to have a client with id 0 in the scene, most of the time it'll be true but if you want to be sure use the isOnwedLocally property or use the lowest id in the scene method
MasterOX 水
MasterOX 水2mo ago
alright, ty!