- Home /
Multiplayer drawing game
Hello,
I am trying to make a multiplayer drawing game using raycasting and network manager.
I was referencing to this tutorial: https://unity3d.college/2017/07/22/build-unity-multiplayer-drawing-game-using-unet-unity3d/
Now I am trying to change the brush from pixel to a brush image (for instance a round one) and also improve raycasting so when we draw fast the brush leaves continious line instead of single pixels.
This is my playerbrush.cs code: using UnityEngine; using UnityEngine.Networking;
public class PlayerBrush : NetworkBehaviour
{
#region Initialization
[Server]
private void Start()
{
var data = PaintCanvas.GetAllTextureData();
var zippeddata = data.Compress();
RpcSendFullTexture(zippeddata);
}
[ClientRpc]
private void RpcSendFullTexture(byte[] textureData)
{
PaintCanvas.SetAllTextureData(textureData.Decompress());
}
#endregion
public float rayDistance;
public Color[] colors;
public Texture2D imageMap;
private int FindIndexFromColor(Color color)
{
for (int i = 0; i < colors.Length; i++)
{
if (colors[i] == color)
{
return i;
}
}
return -1;
}
private void Update()
{
RaycastHit hit;
if (Input.GetMouseButtonDown(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, rayDistance))
{
var pallet = hit.collider.GetComponent<PaintCanvas>();
if (pallet != null)
{
Debug.Log(hit.textureCoord);
Debug.Log(hit.point);
Renderer rend = hit.transform.GetComponent<MeshRenderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
return;
Texture2D texture = rend.material.mainTexture as Texture2D;
Vector2 pixelUV = hit.textureCoord;
pixelUV.x *= texture.width;
pixelUV.y *= texture.height;
Vector2 tiling = rend.material.mainTextureScale;
Color color = imageMap.GetPixel(Mathf.FloorToInt(pixelUV.x * tiling.x), Mathf.FloorToInt(pixelUV.y * tiling.y));
int index = FindIndexFromColor(color);
if (index >= 0)
CmdBrushAreaWithColorOnServer(pixelUV, ColorPicker.SelectedColor, BrushSizeSlider.BrushSize);
BrushAreaWithColor(pixelUV, ColorPicker.SelectedColor, BrushSizeSlider.BrushSize);
}
}
}
[Command]
private void CmdBrushAreaWithColorOnServer(Vector2 pixelUV, Color color, int size)
{
RpcBrushAreaWithColorOnClients(pixelUV, color, size);
BrushAreaWithColor(pixelUV, color, size);
}
[ClientRpc]
private void RpcBrushAreaWithColorOnClients(Vector2 pixelUV, Color color, int size)
{
BrushAreaWithColor(pixelUV, color, size);
}
private void BrushAreaWithColor(Vector2 pixelUV, Color color, int size)
{
for (int x = -size; x < size; x++)
{
for (int y = -size; y < size; y++)
{
PaintCanvas.Texture.SetPixel((int)pixelUV.x + x, (int)pixelUV.y + y, color);
}
}
PaintCanvas.Texture.Apply();
}
}
There is no errors but the drawing doesn't work at all. Could anyone help me fix this problem?
i am also working on same kind of game can u please help me with rounds and turns.. i am stucked here
Your answer
Follow this Question
Related Questions
Raycasting over Photon Unity Networking 1 Answer
unity raycast layers in Multiplayer 0 Answers
Multiplayer Kill Counter? 2 Answers
Draw shape with mousedown 1 Answer
RaycastHit2D hit to texture coordinate 0 Answers