Unet IsLocalPlayer not working
I'm quite new to Networking and i've been using some sample assets and tutorials to get started. My problem is that when I do an IsLocalPlayer check in my script that controls the parameters for my animator it only works when I do !IsLocalPlayer, as if it recognises my animator as another player.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Animations : NetworkBehaviour {
private Animator anim;
private float vert;
private float Hor;
void Start(){
anim = GetComponent<Animator>();
}
void Update(){
if (isLocalPlayer)
{
vert = Input.GetAxis("Vertical");
Hor = Input.GetAxis("Horizontal");
anim.SetFloat("JogFWD", vert);
anim.SetFloat("Strafe", Hor);
Debug.Log("LocalPlayer");
}
}
}
Looking at your screenshots I see you're calling 'IsLocalPlayer' from the 'Animations' script, however I don't see that script in your 'Robot $$anonymous$$yle' Component list. Is the 'Animations' script attached to a different GameObject?
@$$anonymous$$aldrisRelm Oops! I can't believe i missed that, thanks! However this may be for another question, but now that players are controlling their own animations, they can not be seen on the other clients view. any suggestions as to what could be causing this?
@$$anonymous$$aldrisRelm okay I haven't changed anything and it is not working anymore, script is correctly attached and the setup is the same as the screenshot. do you think it may be a bug with unity?
Glad to hear it worked for ya, I'll make an 'answer' post towards your second question.
Try base.isLocalServer ins$$anonymous$$d and see if that works.
Answer by KaldrisRelm · Sep 03, 2015 at 02:45 AM
Towards your initial question:
Looking at your screenshots I see you're calling 'IsLocalPlayer' from the 'Animations' script, however I don't see that script in your 'Robot Kyle' Component list. Is the 'Animations' script attached to a different GameObject?
Towards your second question regarding playing animations on all players:
Players can't communicate to other players in UNET without going through the Server first. Take the below case for instance:
Player one hits space, plays the jump animation.
To sync this across all characters, the Player (using the isLocalPlayer function) would submit a Command to the Server, saying they want to jump. The Server would then send a ClientRPC (after doing some checking if necessary first) stating that the GameObject is playing the jump animation. All players receive the ClientRPC and play the jump animation for the object:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerJump : NetworkBehaviour
{
public Animator PlayerAnimator;
void Update ()
{
if(isLocalPlayer)
{
if(Input.GetKeyDown (KeyCode.Space))
{
CmdJumpPlayer();
}
}
}
[Command]
void CmdJumpPlayer()
{
RpcJumpPlayer ();
}
[ClientRpc]
void RpcJumpPlayer()
{
PlayerAnimator.SetBool ("Jump", true);
}
}
You should be able to convert the above to your script, but let us know if you need any further help.