- Home /
Question by
ElijahOmotosho · Dec 02, 2020 at 12:33 AM ·
c#gameobjectmultiplayermousegameobjects
Coding Help
In the process of Creating and Checkers game but I keep running into problems when it comes to the movement of the piece, it happens every time I click on a piece, the piece on the board at 0,0 will always be the selected piece and moves when it is not meant to please can anyone help with my code. Using Unity 2018.4.27f.
Moving Pawn C# Script:
// Start is called before the first frame update
void Start()
{
mouseOver = lom.mouseOver;
}
// Update is called once per frame
void Update()
{
int x = (int)lom.mouseOver.x;
int y = (int)lom.mouseOver.y;
//If it is Red turn
if (Input.GetMouseButtonDown(0))
{
ifRclicked = true;
if (ifRclicked == true)
{
ifRclicked = false;
SelectPawn(x, y);
}
}
if ((Input.GetMouseButtonDown(0)))
{
ifLclicked = true;
if (ifLclicked == true)
{
ifLclicked = false;
TryMove((int)startDrag.x, (int)startDrag.y, x, y);
}
}
}
private void TryMove(int x1, int y1, int x2, int y2)
{
//Mutiplayer Support
startDrag = new Vector2Int(x1, y1);
endDrag = new Vector2Int(x2, y2);
selectedPawn = gb.p[x1,y1];
//Check if out of Bounds
//Check if there is a selected piece
gb.MovePawn(selectedPawn, x2, y2);
}
}
LocationOfMouse C# Script:
private void UpdateMouseOver()
{
//If its my turn
if (!Camera.main)
{
Debug.Log("Can't Locate Main Camera");
return;
}
RaycastHit h;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out h, 25.0f, LayerMask.GetMask("Board")))
{
mouseOver.x = (int)(h.point.x - Of.x);
mouseOver.y = (int)(h.point.z - Of.y);
}
else
{
mouseOver.x = -1;
mouseOver.y = -1;
}
}
// Start is called before the first frame update
void Start()
{
Of = offset.bOffset;
Of.x = Of.x - 0.5f;
Of.y = Of.z - 0.5f;
}
// Update is called once per frame
void Update()
{
UpdateMouseOver();
Debug.Log(mouseOver);
}
Generating Board C# Script:
//Generate Board
public void CreateBoard()
{
//Red Team
for (int y = 0; y < 3; y++)
{
//boolean for the middle row
bool mRow = (y % 2 == 0);
for (int x = 0; x < 8; x+=2)
{
GeneratePawn((mRow)? x : x + 1, y);
}
}
//Black Team
for (int y = 7; y > 4; y--)
{
//boolean for the middle row
bool mRow = (y % 2 == 0);
for (int x = 0; x < 8; x += 2)
{
GeneratePawn((mRow) ? x : x + 1, y);
}
}
}
//Generate Pawn
public void GeneratePawn(int x, int y)
{
bool isRed = (y > 3)?false:true;
GameObject go = Instantiate((isRed)?rp:bp) as GameObject;
go.transform.SetParent(transform);
Pawns pawn = go.GetComponent<Pawns>();
p[x, y] = pawn;
MovePawn(pawn,x,y);
}
public void MovePawn(Pawns p, int x, int y)
{
p.transform.position = (Vector3.right * x) + (Vector3.forward * y) + bOffset;
}
// Start is called before the first frame update
private void Start()
{
//Generating the Board
CreateBoard();
}
// Update is called once per frame
void Update()
{
}
Comment
Your answer
Follow this Question
Related Questions
If Check Colliding GameObject Variable 1 Answer
C# Gameobject Rigidbody Mouse Collision 1 Answer
when mouse is on a object do this 2 Answers
Move the player(cube) to the enemy(cube) by clicking on enemy? 1 Answer
Destroying DontDestroyOnLoad 1 Answer