1
\$\begingroup\$

I am making a board game in unity 4.3 2d similar to checkers. I have added an empty object to all the points where player can move and added a box collider to each empty object.I attached a click to move script to each player token. Now I want to assign valid moves. e.g. as shown in picture... Players can only move on vertex of each square.Player can only move to adjacent vertex.Thus it can only move from red spot to yellow and cannot move to blue spot.There is another condition which is : if there is the token of another player at the yellow spot then the player cannot move to that spot. Instead it will have to go from red to green spot. How can I find the valid moves of the player by scripting.

enter image description here

I have another problem with click to move. When I click all the objects move to that position.But I only want to move a single token. So what can i add to script to select a specific object and then click to move the specific object.Here is my script for click to move.

var obj:Transform;

private var hitPoint : Vector3;

private var move: boolean = false;

private var startTime:float;

var speed = 1;


function Update ()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
var hit : RaycastHit; // no point storing this really
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 10000))
{
hitPoint = hit.point;
move = true;
startTime = Time.time;
}
}

if(move)
{
obj.position = Vector3.Lerp(obj.position, hitPoint, Time.deltaTime * speed);
if(obj.position == hitPoint)
{
move = false;
}
}
}`
\$\endgroup\$
2
  • \$\begingroup\$ For your first question: you should be storing the grid in a 2D array for ease of use... That way to check the tile to the top left of your current tile you would do - TileArray[myX + 1, myY + 1]. But you would of course need to check that you are not outside of the array bounds beforehand... \$\endgroup\$
    – Savlon
    Jun 11 '14 at 12:52
  • \$\begingroup\$ Second question: you could create a main GameController and have a variable "Tile selectedTile" (presuming you have a Tile class) and when you click on a tile it sets the selectedTile variable to the clicked tile... Then in your code only move the selectedTile... Not sure if you understand the concept but that's the way I would do it \$\endgroup\$
    – Savlon
    Jun 11 '14 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.