Respawn Objects | VRChat Udon Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right so let's say we have a bunch of synced objects that people have tossed around and we want a really good way to just reset them back to their original position fortunately that's pretty easy especially with the voc object sync component which is what allows us to sync things in different places but there are a couple steps that we have to go through to do that i've set up a frankly over-engineered chess set here in order to actually like showcase this every one of these pieces is a synced object but i also set up a little script on each one to snap it to a specific rotation and position so that it actually matches the chess set completely unnecessary for this video but we won't be using that at all this is just be the visual explanation of a way to implement this what we are going to be using is this box right here the reset button so we need to set up an udon behavior to go on this to actually do our resetting so let's start by doing a add component boot on behavior and we could do new program here but i'll put it somewhere random so we'll just make our script in our folder so we go to vr chat udon udon graph program asset and i'll just call this respawn objects now if we go back to our reset box here i'll just put the graph into the slot on here and then we actually have access to our graph and everything let's open this up and get started we hit open udon graph it'll open up here and the first thing that we need is actually our group of object sinks that we're going to be respawning so in our variables we'll do object sync and you see we have vrc object sync and the one with brackets is an array so that lets us have multiple obviously new variable isn't very obvious what this is so i'll just call this object syncs in a drop down here you'll see the public button and if we hit that and press compile you'll see it shows up in our inspector right down here right now there's nothing in here but we have 32 chess pieces so i'll just set this to be 32 and fill these in in a bit for now i'll press shift space to full screen my graph and we can get started since we're respawning everything when somebody clicks this we're going to use the interact event so hit space to search and type interact and event interact is what we want now we basically want to say for every single one of these respawn it but in order to do that we're first going to have to understand ownership ownership is the way that vrchat handles syncing information across two other players basically one player is in charge of this information or that information and every single synced object is owned by typically the person who was the last person to pick it up if no one picked it up it's just the person who's been in the world the longest now we're able to declare specifically who we want to be the owner for one thing or another and we're going to do that so we become the owner of every single one of the objects in order to actually respawn it because we can't declare oh this object has respawned if we aren't the person who is in charge of declaring information for that object might be a might sound a little bit complicated but basically it's just one more step in our process that needs to happen so we're going to do this for every single one of our objects so we're going to be using a for loop we'll type in four and you see here this gives us three things a start and end and a step step basically means we're counting up by one we don't want to change that and we want to pass this the number of vrc object syncs that we actually have so we'll drag out from object syncs and we'll drag out from here to do length and we want to do the first one get space length all the others don't use them not not for this implementation now that we have this number we can plug this into end and this will basically count up from 0 until 32 is like our length up here so it'll actually stop when it gets to 32 which is what we want because we only technically have 31 because we start at zero if we plug this in here this for loop will run whenever we click on this now that we have our for loop set up we actually needed to do something so we're going to get another reference to our object sinks over here just so there's less noodly mess and drag out from here and type get get allows us to say a specific number and like if we say five it'll get us the fifth object sync in that array and we can do something with that we're going to plug this into here and that basically lets us do this code that comes out of body for every single one of the things that we're doing and again firstly we need to become the owner of this object and since syncing is done on a game object basis we need to get the game object from this object sync so we can drag out from here and type game object the first thing that shows up is get game object now we need to pass this into a event called networking set owner this takes two variables a player that will become the owner of it and the object that we're respawning obviously we put our game object into here now we need to get the local player or the person who pressed this button ie you fortunately that's the same as networking set owner we just do networking local player then we can plug this variable into player and stack everything up neatly in a way that might be readable now that we've become the owner of this object we just need to respawn it which is as simple as dragging out from where we're already getting this object and typing respawn just plug that into right after set owner and there we have it that's our entire graph and it's respond really all of the work of this graph is just becoming the owner of this object right here so that we can safely declare what is happening to this object shift space again to unfull screen and now we get to do the fun part which is going to our scene and manually dragging in every single one of these things oh did i say fun i i didn't mean that sorry i lied i'll probably speed through this and there we go that's all of our synced objects placed in this array and now we can just hit play and test it out i'm using the creator companion right now uh and that comes with client sim vr chat's built-in way of testing during play mode if you're not using the creator companion be sure to be using sign mu it's literally the same thing but client sim is him rewriting all of it so that's better let's close the menu and if we run up to our stuff here you'll see that i can move them around and you see how it snapped earlier as i was explaining and it the rotations also snap to 45 degree angles so if i slightly rotate that it snaps forward but obviously what we're here for is respawning it so let's say oh we finished the game we can hit reset and everything goes back to its original positions now if you're just working on graph that's it that's all you needed to do but now i'm going to go ahead and set this up in udon sharp as well because that's another system people might want to use so i can leave play mode and let's go ahead and actually make our script here same as the object respawn graph we just do right-click and create but click u-sharp script and i'll type respawn objects and since we already have one here for graph i'll just call this sharp now you don't need to call this sharp obviously if you're not doing the graphs one but this just lets me distinguish it now since we've made a new script here we just have to let it compile and by the time that's done we can go ahead and double click this and open it up in our editor first though let's actually open up our udon graph page for the respawn objects because this will let us compare directly between what we've done here and what we're going to do in udon sharp all right with both of these open we can actually get to work now the first thing that i always do when making a udon sharp script or any script for that matter is put it in a namespace because if two people have the same script named something uh like button2 or something like that then it's just going to break but if you put in a namespace then it's safely concealed within that so you just do that by typing namespace and i'll call this falgen because it's mine open curly brackets and close curly bracket to hold the whole script now if anyone else writes respawn object sharp is not going to conflict with mine unless they're i guess intentionally putting it in my namespace in which leave me alone now we don't need our start event so we can go ahead and clear out all that and the first thing that we want to do as we did over here is make our variable so we're going to do a public vrc object sync but as you see it's not actually filling here so we need to import the namespace that this is actually from now you use namespaces up here instead of declaring your script in one so we'll just do using vrc dot sdk three dot components and that's the name space that the vrc object sync variable is inside of it's not super intuitive but some ides will autofill this such as rider or if you have different plugins for the one that you're using it should work perfectly fine otherwise you just have to remember this one now we need our object sync to be an array so open close square brackets and i'll just call this object syncs yet again we're going to be using the interact event and since it's a vr chat event we can get an autofill by doing public override and type interact and we can just hit enter and autofills the whole thing we won't be using base.interact in fact it will break if we try to do that so we'll just clear that line and we're going to start up our for loop now for loops are visually different than they are in the graph because they're not a box but i'm going to type this out here quick and then explain what it's doing now to quickly explain what the for loops is doing we're making a new variable i which is the same thing as the start over here it's just a zero step is going up by one which is what the i here is now i plus plus that's just a shorthand for i equals i plus one or i plus equals one and i plus plus is just the shortest way of writing that i less than object sync.length is basically the same thing as the end point so while i is less than objects.length we increase i by one and then we run this again basically the same thing as it works over here just visually explained differently so the first thing we do is set ourselves to be the owner so we'll do networking dot set owner and of course first we pass the networking dot local player comma and then we're going to get our current object sync so object syncs square brackets i dot game object and there we go that's us getting the game object from the current object sync that we have and setting it to be owned by the local player or the person who clicked the interact event next up we just do object syncs square brackets i dot respawn and that is of course a reference to the object synthetic and just telling it to respawn and this is actually our whole script this is completely done so ctrl s to save and if we head back into unity we can just go and assign everything the same way we did earlier now on our reset button you see we still have our array here of our 32 object sinks but we don't actually need our graph one at all we just need our u sharp one so what we can do is we can actually drag all of these in at once instead of manually doing it so first we just lock the inspector so if i click on something else it doesn't change that it's just done by clicking the lock button up here and we can shift click all of the objects that have an object sync on them and drag it into the name object syncs and it populates it completely right there i've already tested it working here in the graph with the editor so i'll just boot it up into vr chat here to show it working with udon sharp all right load it in we can run up to our chess set and i can drag our pieces around and they snap the same way that they did in the editor but now that we have a bunch of them in different positions i can go and click the reset button and they all get snapped back to their original positions there you go that is how to do object responding for synced objects in both graph and udon sharp i know it's been a while since the last video but the chess set here and the respawning system will be up on my patreon uh the chess that will be up on booth and i hope you enjoyed we'll see you next time [Music] you
Info
Channel: Vowgan VR
Views: 2,938
Rating: undefined out of 5
Keywords: VRChat, Tutorial, Udon, UdonSharp, U#, Unity, Unity3D
Id: cAQWVq7o5x0
Channel Id: undefined
Length: 13min 41sec (821 seconds)
Published: Wed Aug 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.