Hello,
so I currently have a random generated world made out of 2d tiles, and I want to whenever the mouse hovers over a tile check if the tile is free which means that the tile is not closed off by other tiles so that you will be able to for example ‘mine the tile away and gather it’s resources’. So I want to check if the player is able to ‘mine’ away that certain tile only if the tile is able to be ‘mined’ because you don’t want the player to remove tiles that are deep underneath the ground…
So I made this script:
#pragma strict
var Player : GameObject;
var hit : RaycastHit2D;
function OnMouseOver () {
Player = GameObject.Find('Player(Clone)');
hit = Physics2D.Raycast (Player.transform.position, gameObject.transform.position);
Debug.DrawLine(Player.transform.position, gameObject.transform.position, Color.green);
if (hit.collider.gameObject.tag == 'Tile') {
if (hit.collider.gameObject.transform.position == gameObject.transform.position) gameObject.GetComponent(SpriteRenderer).color = Color.green;
else gameObject.GetComponent(SpriteRenderer).color = Color.red;
}
}
function OnMouseExit () {
gameObject.GetComponent(SpriteRenderer).color = Color.white;
}
It checks if a tile is able to be ‘mined’ by using a raycast and then after that giving it a green color overlay indicating that it is able to be ‘mined’ or a red overlay indicating that it is not able to be ‘mined’.
But my problem is is that the ray is acting very weird, it doesn’t do what I want it to do it does not give good coordinates of tiles and it just acts really strange.
Please take a look at my code and tell me what problem there is,
Thanks in advance!
ALSO: The Debug.DrawLine function is not working. I don’t see a green line appear on the screen so that also does not help D: