Sorry for coming up with two questions in 4 days, but something strange is happening again on my game
So, my game is basically a “Pizza restaurant co-op game” where you can obviously make pizzas and deliver them. You can spawn and pick the toppings to make the pizza, and for picking the objects, the game uses a raycast to hit and detect the rigidbodies so it can disable the topping’s gravity and move it as the player wants to.
Everything works great, the player can spawn the objects, but when he tries to pick them up, the rigidbody does not appear on the variable, meaning it’s not being referenced. The function does get called, as i recieve that “grab” message on the console, but the variable just doesn’t get the rigidbody and for that reason i can’t grab the object.
These are the lines where the player launch the raycast and gets the rigidbody:
// LAUNCH RAYCAST
Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
if (Physics.Raycast(CamRay, out RaycastHit HitInfo, PickRange, PickMask))
{
CurrentObj = HitInfo.rigidbody;
CurrentObj.useGravity = false;
Debug.Log("grab");
}
Btw, i’m also using ClientNetworkTransform and the NetworkRigidbody component in all the objects i want to spawn and grab.
I think it is very important to mention that the code provided there and also the code for spawn the toppics are all in an ServerRpc function. I don’t know if i can’t do that in ClientNetworkTransform because it’s Owner Authoritive…i think…
Hi @AngelHYYPE09 , have you already had a look at the examples in the official ClientDriven sample? (Download here)
It show how to pick objects up in a multiplayer environment, maybe you’ll find an example to follow.
Does this help?
Hi, so i saw that and it does help since now i get the rigidbody that the client wants to grab, but it just doesn’t move.
I don’t know what to do at this point, it’s kind of confusing because everything works fine but the object doesn’t change it’s position at any moment, while the host can do that.
If the host can move the object but other clients can’t, it means that either you’re managing the object in a server-authoritative way or other clients don’t have ownership over it.
So i tried to spawn the objects with ownership, but i get an error saying “The name ‘clientId’ does not exist in the current context”. Do i need to do something else in my code before using clientId?
I used this line:
Mynetobj.GetComponent<NetworkObject>().SpawnWithOwnership(clientId);
Hey so i just solved it, for anyone in my situation, just create a function with a serverRpc and a ulong in the object script you want to pick, like this:
public void GiveOwnershipServerRpc (ulong clientId)
Inside that function, put this:
GetComponent<NetworkObject>().ChangeOwnership(clientId);
And in your player script, when you want to pick an object, do something like this:
// Grab object
public void GrabFunction()
{
// Launch raycast
Ray CamRay = playerCamera.ViewportPointToRay(new
Vector3(0.5f, 0.5f, 0f));
// if raycast hits an object...
if (Physics.Raycast(CamRay, out RaycastHit HitInfo,
PickRange, PickMask))
{
HitInfo.collider.gameObject.GetComponent<ObjectScript>().GiveOwnershipServerRpc(NetworkManager.Singleton.LocalClientId);
}
}
I’m not sure if this implementation is correct, but it did work for me so i hope it works for someone! Thanks for your help!
With that being said, i have another problem, it’s with the spawn of the objects, so i have a ServerRpc function that spawns the ingredients depending on what machine the player interacts with, almost all the machines work, but the last one, which is for the dough dispenser, doesn’t work, even if it has almost the exact same code! Maybe it’s some bug of the netcode or im doing something wrong, which is probably the case since i am pretty new in this stuff. Here’s my code:
[ServerRpc(RequireOwnership = false)]
private void AllgrabServerRpc()
{
Ray CamRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
Debug.Log("does this?");
if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pepperoni, PickRange, PickMask_Pepperoni))
{
Pepperonimachinescript.inst.OnPressedPepperoni();
Transform SpPepper = Instantiate(Pepobj, TargetSpawnPp.transform.position, Pepobj.transform.rotation);
SpPepper.GetComponent<NetworkObject>().Spawn(true);
}
if (Physics.Raycast(CamRay, out RaycastHit HitInfo_mushroom, PickRange, PickMask_Mushroom))
{
Mushroommachinescript.inst.OnPressedMushroom();
Transform SpMush = Instantiate(Mushobj, TargetSpawnMush.transform.position, Mushobj.transform.rotation);
SpMush.GetComponent<NetworkObject>().Spawn(true);
}
if (Physics.Raycast(CamRay, out RaycastHit HitInfo_blackolive, PickRange, PickMask_Olive))
{
Bolivemachinescript.inst.OnPressedOlive();
Transform SpBlack = Instantiate(Oliveobj, TargetSpawnOlive.transform.position, Oliveobj.transform.rotation);
SpBlack.GetComponent<NetworkObject>().Spawn(true);
}
if (Physics.Raycast(CamRay, out RaycastHit HitInfo_pineapple, PickRange, PickMask_Pineapple))
{
Pineapplemachinescript.inst.OnPressedPineapple();
Transform SpPine = Instantiate(Pineobj, TargetSpawnPine.transform.position, Pineobj.transform.rotation);
SpPine.GetComponent<NetworkObject>().Spawn(true);
}
if (Physics.Raycast(CamRay, out RaycastHit HitInfo_doughdispense, PickRange, PickMask_Dough))
{
DoughDispense.inst.OnPressedDough();
Transform SpDough = Instantiate(Doughobj, TargetSpawnDough.transform.position, Doughobj.transform.rotation);
SpDough.GetComponent<NetworkObject>().Spawn(true);
}
//This
}
Sorry if the code is a mess hahah