I am working on a 3D game and I’d like to be able to play it with my friends, though this is my first time giving networking a shot. I have a player and he is able to shoot a physics projectile. When I load up the game, the host is able to see his and the client’s projectiles fine, but none of the projectiles spawn on the client side.
throw script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class PlayerThrow : NetworkBehaviour
{
public GameObject[] ThrowObjects;
GameObject equippedThrowObject;
GameObject equippedObjectInHand;
public Transform handPos; //where object is thrown from
public Transform cameraTransform;
public PlayerMotor motor;
Throwable throwData;
[SyncVar] float throwTime;
[SyncVar] float currentThrowTime;
private KeyCode[] keyCodes = {
KeyCode.Alpha1,
KeyCode.Alpha2,
KeyCode.Alpha3,
KeyCode.Alpha4,
KeyCode.Alpha5,
KeyCode.Alpha6,
KeyCode.Alpha7,
KeyCode.Alpha8,
KeyCode.Alpha9,
};
private void Start()
{
equippedThrowObject = ThrowObjects[0];
throwData = equippedThrowObject.GetComponent<ThrowControl>().throwableData;
throwTime = throwData.ThrowTime;
equippedObjectInHand = Instantiate(throwData.handGameObject, handPos);
}
public virtual void Update()
{
if (!isLocalPlayer)
return;
#region equip
for (int i = 0; i < ThrowObjects.Length; i++)
{
if (Input.GetKeyDown(keyCodes*))*
{
Destroy(equippedObjectInHand);
TargetRpcEquip(i);
}
}
#endregion
}
[Command]
void TargetRpcEquip(int equipIndex)
{
equippedThrowObject = ThrowObjects[equipIndex];
throwData = ThrowObjects[equipIndex].GetComponent().throwableData;
throwTime = throwData.ThrowTime;
equippedObjectInHand = Instantiate(throwData.handGameObject, handPos);
}
public virtual void FixedUpdate()
{
if (isLocalPlayer)
{
if (Input.GetMouseButton(0) && currentThrowTime <= 0f)
{
CmdThrow();
currentThrowTime = throwTime;
motor.velocity = new Vector3(motor.velocity.x - throwData.pushForce * transform.forward.x, //pushes back player when item is thrown
motor.velocity.y - throwData.pushForce * transform.forward.y,
motor.velocity.z - throwData.pushForce * transform.forward.z);
}
else currentThrowTime -= Time.deltaTime;
}
}
[Command]
void CmdThrow()
{
GameObject objectThrown = Instantiate(equippedThrowObject, handPos.position, cameraTransform.rotation);
NetworkServer.Spawn(gameObject, connectionToClient);
print(“spawn call”);
RpcThrowReply();
}
[ClientRpc]
void RpcThrowReply(){
print(“Spawned”);
}
}
manager:
[198099-screenshot-2022-07-27-152104.png|198099]*
player:
[198100-screenshot-2022-07-27-152215.png|198100]*
the player also has a child network transform that is attached to the camera object in the player, so the player will able to see other players rotate up and down. The projectile has a network identity and transform, as well.
Let me know if you have further questions. Thank you!
*
*