- Home /
2D Tiles Raycast Problem
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:
I have noticed that this may be a unity bug but I'm not sure, can someone help me out please?
I have noticed that this may be a unity bug
It is best to assume that the issue is with your code and not a Unity problem. Of the thousands of questions I've answered and the tens of thousands of questions I've read, only a couple were Unity bugs. Assu$$anonymous$$g it is your bug also keeps you looking for the answer.
Answer by robertbu · Jul 23, 2014 at 01:21 PM
You have a couple of issues here. First, this code is not necessary. Unity uses Raycasting to determine when a mouse is over an object in order to trigger the OnMouseOver() callback. If your tile is behind other tiles (from the view of the camera), then you will not get an OnMouseOver().
The second issue is that Raycast takes a position and a direction. You are passing it two positions. The Raycast() is using the second position as a direction. There is a Physics2D.Linecast that you can use to cast a line between two positions.
And finally, you are not checking if hit.collider is null. If the Raycast() fails, then hit.collider will be null and therefore doing 'hit.collider.gameObject.tag' will generate a null reference exception. Minor point, you can do 'hit.collider.tag'.