How to stop RaycastHit2D from using the last object in the hierachy?

Hello all,

I am having a few issues with my raycasting and I was wondering if anyone could help.

Basically what I am doing is using the raycast to gather info on objects that have been clicked on. When you click on an object all the child objects that contain tags are gathered and the info is used to change animations and collections of sprites etc.

The problem I am having is when the object is clicked on instead of returning the object clicked on it is returning the object that is last in the hierarchy.

Here is my raycast function:

public virtual void mouseRayInput()
	{

		if(Input.GetMouseButtonDown(0))
		{
			mouseScreenPos = Input.mousePosition; //Gets the mouse position in 2D space
			mouseWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos); //converts 2d mouse position into 3d world
			oldMouseWorldPos = mouseWorldPos; //stores the last known mouse click position
			hasClicked = true;

			RaycastHit2D hit = Physics2D.Raycast(mouseWorldPos, Vector2.zero);

			if(hit)
			{
				colliderName = hit.collider.tag;
				print ("Your object is = " + hit.collider.tag);
				clickedInsideBounds = true;
			}
			else
			{
				clickedInsideBounds = false;
			}
		}
		else if(Input.GetMouseButtonUp(0))
		{
			hasClicked = false;
		}
	}

and here is my clicked inside sprite function:

public void clickedInsideSpriteSequence()
{
	string [] arrayOfTags = new string[transform.childCount]; //Sets up a local array to store the child tags in
	int numOfChildrenCount = 0;

	if(clickedInsideBounds == true && hasClicked == true && colliderName != "Player")
	{
		rayCast = false;
		//Gets the child of the sprite this script is attached to and returns its tag
		foreach (Transform child in transform)
		{
			arrayOfTags[numOfChildrenCount] = child.tag; //store the tag in the array
			numOfChildrenCount++;
		}
		print (arrayOfTags[0]);
		 //Sets each property to be that of the children under the object clicked on
		Animation_Class.libraryName = clickableSprite.tag;
		Animation_Class.collectionName = arrayOfTags[0];
		Animation_Class.currentAnim = arrayOfTags[1];
		Animation_Class.currentIdleAnim = arrayOfTags[2];
		Animation_Class.numOfAnims = int.Parse(arrayOfTags[3]); //converts string to integer using int.parse

		changeLibrary(libraryName, collectionName, currentAnim + animNum as string);
		Animation_Class.playSequence();
		sequencing = true;

		print (collectionName);
		print (currentAnim);
		print (numOfAnims);
		//Destroy(this); //remove the script preventing any more animation changes
	}

}

I read somewhere briefly about using collider.raycast but then I read somewhere else that it doesn’t really work??

Has anyone had this type of issue before, if so any help would be appreciated :slight_smile:

Cheers!!

I have tried looking for a solution to this but I am totally stumped and have been for a while. The script is attached to more than one object. Could this be the problem?

1 Answer

1

You need to use RaycastAll instead

RaycastHit2D hit = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);