How to Update Objects Sprite to the Network
Hey there. Im trying to make a 2D online game with "Move to mouse click position" style with player following camera. The movement itself works fine with the Network transform script on Player prefab. On the other hand i manage the moving with changing objects sprite for the angle object moves but the changes in the sprites doesnt apply to the server. so player sees other players moving without any sprite change. i am trying to fix this for 4 days. i read everything i can find but non of them worked.Can you guys please help me with this.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour {
public float angle;
public float ShipSpeed = 7.0f;
public Vector3 localTarget;
public Sprite a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,r,s,t,u,v,y,z,za,zb,zc,zd,ze,zf,zg,zh,zi;
public void FixedUpdate()
{
if (isLocalPlayer) {
Move ();
ChangeSprite ();
}
}
void Move(){
if (Input.GetMouseButton (0)) {
SelectTarget ();
localTarget = Camera.main.ScreenToWorldPoint (Input.mousePosition);
localTarget.z = 0;
}
transform.position = Vector3.MoveTowards (transform.position, localTarget, ShipSpeed);
}
void ChangeSprite(){
angle = Mathf.Atan2 (localTarget.y - bulletSpawn.transform.position.y, localTarget.x - bulletSpawn.transform.position.x) * 180 / Mathf.PI;
if (angle > -95.625f && angle <= -84.375f) {GetComponent<SpriteRenderer>().sprite = a;}
if (angle > -84.375f && angle <= -73.125f) {GetComponent<SpriteRenderer>().sprite = b;}
if (angle > -73.125f && angle <= -61.875f) {GetComponent<SpriteRenderer>().sprite = c;}
if (angle > -61.875f && angle <= -50.625f) {GetComponent<SpriteRenderer>().sprite = d;}
if (angle > -50.625f && angle <= -39.375f) {GetComponent<SpriteRenderer>().sprite = e;}
if (angle > -39.375f && angle <= -28.125f) {GetComponent<SpriteRenderer>().sprite = f;}
if (angle > -28.125f && angle <= -16.875f) {GetComponent<SpriteRenderer>().sprite = g;}
if (angle > -16.875f && angle <= -5.625f) {GetComponent<SpriteRenderer>().sprite = h;}
if (angle > -5.625f && angle <= 5.625f) {GetComponent<SpriteRenderer>().sprite = i;}
if (angle > 5.625f && angle <= 16.875f) {GetComponent<SpriteRenderer>().sprite = j;}
if (angle > 16.875f && angle <= 28.125f) {GetComponent<SpriteRenderer>().sprite = k;}
if (angle > 28.125f && angle <= 39.375f) {GetComponent<SpriteRenderer>().sprite = l;}
if (angle > 39.375f && angle <= 50.625f) {GetComponent<SpriteRenderer>().sprite = m;}
if (angle > 50.625f && angle <= 61.875f) {GetComponent<SpriteRenderer>().sprite = n;}
if (angle > 61.875f && angle <= 73.125f) {GetComponent<SpriteRenderer>().sprite = o;}
if (angle > 73.125f && angle <= 84.375f) {GetComponent<SpriteRenderer>().sprite = p;}
if (angle > 84.375f && angle <= 95.625f) {GetComponent<SpriteRenderer>().sprite = r;}
if (angle > 95.625f && angle <= 106.875f) {GetComponent<SpriteRenderer>().sprite = s;}
if (angle > 106.875f && angle <= 118.125f) {GetComponent<SpriteRenderer>().sprite = t;}
if (angle > 118.125f && angle <= 129.375f) {GetComponent<SpriteRenderer>().sprite = u;}
if (angle > 129.375f && angle <= 140.625f) {GetComponent<SpriteRenderer>().sprite = v;}
if (angle > 140.625f && angle <= 151.875f) {GetComponent<SpriteRenderer>().sprite = y;}
if (angle > 151.875f && angle <= 163.125f) {GetComponent<SpriteRenderer>().sprite = z;}
if (angle > 163.125f && angle <= 174.375f) {GetComponent<SpriteRenderer>().sprite = za;}
if (angle > 174.375f && angle <= 180f) {GetComponent<SpriteRenderer>().sprite = za;}
if (angle >= -179.999f && angle <= -174.375f) {GetComponent<SpriteRenderer>().sprite = zb;}
if (angle > -174.375f && angle <= -163.125f) {GetComponent<SpriteRenderer>().sprite = zc;}
if (angle > -163.125f && angle <= -151.875f) {GetComponent<SpriteRenderer>().sprite = zd;}
if (angle > -151.875f && angle <= -140.625f) {GetComponent<SpriteRenderer>().sprite = ze;}
if (angle > -140.625f && angle <= -129.375f) {GetComponent<SpriteRenderer>().sprite = zf;}
if (angle > -129.375f && angle <= -118.125f) {GetComponent<SpriteRenderer>().sprite = zg;}
if (angle > -118.125f && angle <= -106.875f) {GetComponent<SpriteRenderer>().sprite = zh;}
if (angle > -106.875f && angle <= -95.625f) {GetComponent<SpriteRenderer>().sprite = zi;}
}
public override void OnStartLocalPlayer(){
Camera.main.GetComponent<CameraFollow> ().setTarget (gameObject.transform);
}
}
Have you found a solution? I have a very similar problem.
Answer by NITRONOME · Apr 04, 2018 at 06:37 AM
yes i have. dont use high level api. if you insist on using it. you need to calculate the sprite referance(angle in my case) in a command fuction so the server knows it
Gotcha, thanks! I'll play around with that concept. By itself it hasn't worked, but I think if I combine it with an RpcClient it might do the trick.
Your answer
Follow this Question
Related Questions
Why is my script firing 2 bullets on the client side? 0 Answers
Delay in synchronization in unity3d Unet 0 Answers
Problems syncing players in multiplayer 0 Answers
UNet - sync child transform scale 1 Answer
Trivia Game in multiplayer mode? 0 Answers