Script to face the direction of the mouse only on the LocalPlayer?
So my player controller script works fine on the host side of my multiplayer game, but when I run a build and connect as a seperate client, the client shoots in whatever direction the mouse is according to the host. Here is my script. Also if I remove the Destroy(this); and just leave return; the client is stuck shooting straight up. Any help is appreciated and please let me know if anymore details are required. Error message: Found no behaviour for incoming [Command:InvokeCmdCmdfaceMouse] on Player(Clone) (UnityEngine.GameObject), the server and client should have the same NetworkBehaviour instances [netId=4]. UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
void Start()
{
if (!isLocalPlayer)
{
Destroy(this);
return;
}
//character base speed
speed = 50;
//setting health
baseHealth = 200;
//health modifiers here
health = baseHealth;
//Get and store a reference to the Rigidbody2D component so that we can access it.
rb2d = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Movement();
playerSlow();
CmdfaceMouse();
if (health <= 0)
{
RpcRespawn();
}
}
[Command]
void CmdfaceMouse()
{
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(
mousePosition.x - transform.position.x,
mousePosition.y - transform.position.y);
firePoint.transform.up = direction;
}
}
Answer by meat5000 · Feb 24, 2018 at 06:50 AM
You need the isLocalPlayer tag on the code for the actual direction handling and SyncVar the rotations. Alternatively handle this in a Non-networked script and output the results to a separate Networked and SyncVar'd script. Default behaviour then is to read the rotations from the network unless that object is the local player. https://unity3d.com/learn/tutorials/s/multiplayer-networking
It appears that you are using isLocalPlayer to Destroy the script when not the local player. This causes a difference in the number of existing Networked scripts across the system and the system throws a fit. The number of networked scripts should remain consistent across each device for networked objects.
Your answer
Follow this Question
Related Questions
TrailRender not showing in all clients using Mirror networking 1 Answer
Build errors when using Photon 2 ,Build errors after using Photon 0 Answers
How can I send update to all clients from one client in Multiplayer game in Unity3d? 0 Answers
How to decide an asynchronous multiplayer solution for a turn based mobile chess game? 0 Answers
RPC sending Failed 0 Answers