Search Unity

Spawning object with wrong color

Discussion in 'Multiplayer' started by Pickelzz, Jul 24, 2017.

  1. Pickelzz

    Pickelzz

    Joined:
    May 5, 2017
    Posts:
    2
    This my is first forum post so I apologize for any format errors.

    In my game, player's are an outline of a circle. On the press of a button, the circle expands until the user presses the button again. After that, a filled in circle is supposed to spawn with the players color at the original position the user touched and slowly expand until it reaches the outline circle and fills in the shape. The problem I'm having is when the circle spawns it's not the player color. Server side the colors are correct, just not client side. Heres my code, apologies for how messy it is. I'm kind of in a "Just get it working" phase right now.


    Any help would be appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class Shape_Script : NetworkBehaviour
    8. {
    9.     private Vector3 touchPosition;
    10.     private Vector3 originalScale;
    11.     Transform playerTrans;
    12.     [SyncVar(hook = "expandHook")]
    13.     Vector3 playerScale;
    14.     [SyncVar(hook = "fillHook")]
    15.     Vector3 fillScale;
    16.     [SyncVar]
    17.     public float extendRate;
    18.     [SyncVar]
    19.     public float fillRate;
    20.     [SyncVar]
    21.     public Color playerColor;
    22.     public GameObject fillSprite;
    23.     public GameObject playerPrefab;
    24.     private GameObject fillSpriteClone;
    25.     [SyncVar]
    26.     private Color fillSpriteColor;
    27.     public ParticleSystem stunPrefab;
    28.     [SyncVar]
    29.     public float stunTime = 5f;
    30.     [SyncVar]
    31.     private bool canScale = false;
    32.     private bool canCapture;
    33.     //[SyncVar]
    34.     //bool isVisible = true;
    35.     [SyncVar]
    36.     private int playerScore;
    37.     bool alreadyScaled;
    38.     GameObject[] playerPrefabs;
    39.     NetworkHash128 playerAsset = new NetworkHash128();
    40.     public int PlayerScore
    41.     {
    42.         get
    43.         {
    44.             return playerScore;
    45.         }
    46.         set
    47.         {
    48.             playerScore = value;
    49.         }
    50.     }
    51.     public bool CanCapture
    52.     {
    53.         get
    54.         {
    55.             return canCapture;
    56.         }
    57.     }
    58.  
    59.  
    60.     // Use this for initialization
    61.  
    62.  
    63.     private void Start()
    64.     {
    65.         playerPrefabs = new GameObject[4];
    66.         GetComponent<SpriteRenderer>().color = playerColor;
    67.         originalScale = transform.localScale;
    68.         fillSprite.transform.localScale = originalScale;
    69.     }
    70.  
    71.     public override void OnStartClient()
    72.     {
    73.    
    74.         //foreach(var prefab in playerPrefabs)
    75.         //{
    76.         //    var i = 0;
    77.         //    if (prefab == null)
    78.         //    {
    79.  
    80.         //        playerPrefabs[i] = fillSprite;
    81.         //        ClientScene.RegisterPrefab(fillSprite, playerAsset);
    82.         //    }
    83.         //    else
    84.         //    {
    85.         //        i++;
    86.         //    }
    87.         //}
    88.      
    89.     }
    90.     private void Expand(Vector3 currentScale)
    91.     {
    92.         if(canScale)
    93.         {
    94.             if (isServer)
    95.             {
    96.                 RpcExpand();
    97.             }
    98.             else
    99.             {
    100.                 CmdExpand();
    101.             }
    102.         }
    103.     }
    104.  
    105.     private void Fill()
    106.     {
    107.             if (isServer)
    108.             {
    109.             RpcFillShape(fillRate);
    110.             }
    111.             else
    112.             {
    113.             CmdFillShape(fillRate);
    114.             }
    115.     }
    116.  
    117.     private void expandHook(Vector3 value)
    118.     {
    119.         this.transform.localScale = value;
    120.     }
    121.     private void fillHook(Vector3 value)
    122.     {
    123.         fillSpriteClone.transform.localScale = value;
    124.     }
    125.  
    126.     void changeVisible(bool value)
    127.     {
    128.         if (isServer)
    129.         {
    130.             RpcChangeVisible(value);
    131.         }
    132.         else
    133.         {
    134.             CmdChangeVisibility(value);
    135.         }
    136.     }
    137.     void defualtScale(Vector3 value)
    138.     {
    139.         if (isServer)
    140.         {
    141.             RpcOriginalScale(value);
    142.         }
    143.         else
    144.         {
    145.             CmdOriginalScale(value);
    146.         }
    147.     }
    148.  
    149.     void setColor(Color playColor)
    150.     {
    151.        if(isServer)
    152.         {
    153.             RpcSetFillColor(playColor);
    154.         }
    155.        else
    156.         {
    157.             CmdSetFillColor(playColor);
    158.         }
    159.     }
    160.  
    161.  
    162.     // Update is called once per frame
    163.     void Update()
    164.     {
    165.         //This detects where the player touched and if he touched a player or not.
    166.         if (!isLocalPlayer)
    167.         {
    168.             return;
    169.         }
    170.         if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began && canScale == false || Input.GetMouseButtonDown(0) && canScale == false)
    171.         {
    172.             Vector3 playerLocation;
    173.             if (Input.GetMouseButtonDown(0))
    174.             {
    175.                 playerLocation = Input.mousePosition;
    176.             }
    177.             else
    178.             {
    179.                 playerLocation = Input.GetTouch(0).position;  
    180.             }
    181.             float x = -2.87f + 5.74f * playerLocation.x / Screen.width;
    182.             float y = -4.75f + 9.5f * playerLocation.y / Screen.height;
    183.             touchPosition = new Vector3(x, y);
    184.             RaycastHit hit;
    185.             Ray ray = Camera.main.ScreenPointToRay(touchPosition);
    186.             if (Physics.Raycast(ray, out hit) && (hit.collider.tag == "Player" && !isLocalPlayer))
    187.             {
    188.                 Debug.Log("Ive Been Hit");
    189.                 var otherPlayer = hit.collider.GetComponent<Shape_Script>();
    190.                 otherPlayer.stunPrefab.Play();
    191.                 otherPlayer.StartCoroutine(Stunned());
    192.  
    193.  
    194.             }
    195.             else
    196.             {
    197.                 StartCoroutine(MovePlayer(touchPosition));
    198.             }
    199.         }
    200.  
    201.  
    202.     }
    203.     //This moves the player to the specified touch position makes them visible and slowly scales them until the player touches again.
    204.     IEnumerator MovePlayer( Vector3 touchposition)
    205.     {
    206.         canCapture = false;
    207.         canScale = true;
    208.         yield return new WaitForSeconds(0.5f);
    209.         if (canScale)
    210.         {
    211.             //changeVisible(true);
    212.             CmdChangePosition(touchposition);
    213.             var originalPosition = touchposition;
    214.             while (Input.touchCount == 0 && Input.GetMouseButtonDown(0) == false)
    215.             {
    216.                 Expand(this.transform.localScale);
    217.                 yield return null;
    218.             }
    219.             canScale = false;
    220.             CmdCreateShape(playerColor,originalPosition);    
    221.             while (!fillSpriteClone.transform.localScale.magnitude.Equals(this.transform.localScale.magnitude))
    222.             {
    223.                 Fill();
    224.                 yield return null;
    225.             }
    226.             canCapture = true;
    227.             yield return new WaitForFixedUpdate();
    228.             //CmdChangeVisibility(false);
    229.             defualtScale(originalScale);
    230.             CmdDestroyClone();
    231.         }
    232.     }
    233.  
    234.     IEnumerator Stunned()
    235.     {
    236.         yield return new WaitForSeconds(stunTime);
    237.     }
    238.  
    239.     private void CmdDestroyClone()
    240.     {
    241.         NetworkServer.Destroy(fillSpriteClone);
    242.     }
    243.  
    244.  
    245.     [Command]
    246.     private void CmdChangeVisibility(bool updatedVisibility)
    247.     {
    248.         this.GetComponent<SpriteRenderer>().enabled = updatedVisibility;
    249.     }
    250.  
    251.     [Command]
    252.     private void CmdSetColor(Color value)
    253.     {
    254.         fillSprite.GetComponent<SpriteRenderer>().color = value;
    255.     }
    256.  
    257.     [ClientRpc]
    258.     private void RpcSetColor(Color value)
    259.     {
    260.         GetComponent<SpriteRenderer>().color = value;
    261.         fillSprite.GetComponent<SpriteRenderer>().color = value;
    262.     }
    263.     [ClientRpc]
    264.     private void RpcChangeVisible(bool value)
    265.     {    
    266.        this.GetComponent<SpriteRenderer>().enabled = value;
    267.     }
    268.     [Command]
    269.     private void CmdOriginalScale(Vector3 value)
    270.     {
    271.         playerScale = value;
    272.    
    273.     }
    274.     [ClientRpc]
    275.     private void RpcOriginalScale(Vector3 value)
    276.     {
    277.         playerScale = value;
    278.     }
    279.     [Command]
    280.     private void CmdChangeScale(bool updatedScale)
    281.     {
    282.        this.canScale = updatedScale;
    283.     }
    284.     [Command]
    285.     private void CmdChangeCapture(bool updatedCapture)
    286.     {
    287.         this.canCapture = updatedCapture;
    288.     }
    289.     [Command]
    290.     private void CmdCreateShape(Color playColor, Vector3 touchposition)
    291.     {    
    292.         fillSpriteClone = Instantiate(fillSprite, touchposition, Quaternion.identity);
    293.         setColor(playColor);
    294.         ClientScene.RegisterPrefab(fillSpriteClone);
    295.         NetworkServer.SpawnWithClientAuthority(fillSpriteClone, this.gameObject);  
    296.     }
    297.     [Command]
    298.     private void CmdExpand()
    299.     {
    300.         playerScale += new Vector3(0.01f, 0.01f)  * Time.deltaTime * extendRate;    
    301.     }
    302.     [ClientRpc]
    303.     private void RpcExpand()
    304.     {
    305.    
    306.        playerScale += new Vector3(0.01f, 0.01f) * Time.deltaTime * extendRate;
    307.     }
    308.     [Command]
    309.     private void CmdFillShape(float fillrate)
    310.     {
    311.        fillScale= Vector3.MoveTowards(fillSpriteClone.transform.localScale, this.transform.localScale, fillRate * Time.deltaTime);
    312.  
    313.     }
    314.     [ClientRpc]
    315.     private void RpcFillShape(float fillrate)
    316.     {
    317.        fillScale = Vector3.MoveTowards(fillSpriteClone.transform.localScale, this.transform.localScale, fillRate * Time.deltaTime);
    318.     }
    319.     [Command]
    320.     private void CmdChangePosition(Vector3 tposition)
    321.     {
    322.         this.transform.localPosition = tposition;
    323.         if (isServer)
    324.         {
    325.  
    326.             RpcChangePosition(tposition);
    327.         }
    328.     }
    329.     [ClientRpc]
    330.     private void RpcChangePosition(Vector3 tposition)
    331.     {
    332.         this.transform.localPosition = tposition;  
    333.     }
    334.  
    335.     [Command]
    336.     private void CmdSetFillColor(Color value)
    337.     {
    338.         fillSpriteClone.GetComponent<SpriteRenderer>().color = value;
    339.     }
    340.     [ClientRpc]
    341.     private void RpcSetFillColor(Color value)
    342.     {
    343.         fillSpriteClone.GetComponent<SpriteRenderer>().color = value;
    344.     }
    345. }
    346.  
    347.  
    348.  
    349.  
    350.  
    351.  
    352.  
     
    Last edited: Jul 24, 2017
  2. benjaminarmstrong

    benjaminarmstrong

    Joined:
    Jul 24, 2017
    Posts:
    1
    What color is the circle suppose to be?