2D Raycasting Woes

I am building a 2d Zelda-like game, where the player moves in all four directions. The player is meant to detect the presence of objects near them, and kneel (play an appropriate animation) towards the object.

In order to do this, I set up a system of Empty game objects just outside of the players box collider, and used them as the anchor points for four Physics.2D raycasts, since anchoring the rays in the player themselves would cause the rays to detect the player’s own collider (there’s probably a more elegant way, but I went with what I could find). The code detected when the ray hit a collider, checked it for the appropriate tag, and if everything checked out the player would play the animation. This was seperate from a system that would simply check for collisions with the player’s collider, which served as a way of affecting a single instance of an object (so that picking up one pot didn’t lift them all, for example). The code looked like this:

//Raycasts to determine if objects tagged Interactable are nearby, activates anim trigger if true
	void ProxiDetect(){
		RaycastHit2D Up = Physics2D.Raycast (upcheck.position, Vector2.up, 1);
		RaycastHit2D Right = Physics2D.Raycast (rightcheck.transform.position, Vector2.right, 1);
		RaycastHit2D Down = Physics2D.Raycast (downcheck.transform.position, Vector2.down, 1);
		RaycastHit2D Left = Physics2D.Raycast (leftcheck.transform.position, Vector2.left, 1);
		if (Up.collider != null && Up.collider.gameObject.tag == "Plant") {
			anim.SetBool ("ObjU", true);
			anim.SetBool ("ObjD", false);
			anim.SetBool ("ObjRL", false);
		}else if (Right.collider != null && Right.collider.gameObject.tag == "Plant") {
			anim.SetBool ("ObjU", false);
			anim.SetBool ("ObjD", false);
			anim.SetBool ("ObjRL", true);
		}else if (Down.collider != null && Down.collider.gameObject.tag == "Plant") {
			anim.SetBool ("ObjU", false);
			anim.SetBool ("ObjD", true);
			anim.SetBool ("ObjRL", false);
		}else if (Left.collider != null && Left.collider.gameObject.tag == "Plant") {
			anim.SetBool ("Kneelleft", true);
			anim.SetBool ("ObjU", false);
			anim.SetBool ("ObjD", false);
			anim.SetBool ("ObjRL", true);
		} 

		if (Left.collider == null || !(Left.collider.gameObject.tag == "Plant")) {
			anim.SetBool ("Kneelleft", false);
		}

		if (Up.collider == null || !(Up.collider.gameObject.tag == "Plant") && Right.collider == null || !(Right.collider.gameObject.tag == "Plant")&& Down.collider == null || !(Down.collider.gameObject.tag == "Plant")&& Left.collider == null || !(Left.collider.gameObject.tag == "Plant")) {
			anim.SetBool ("ObjU", false);
			anim.SetBool ("ObjD", false);
			anim.SetBool ("ObjRL", false);
		}
	}

And it was working fine.

Until I started adding more scenes to my build.

Now, the raycast works…sometimes. Sometimes on load, the player will only recognize the left-ray, and will ignore the rest, sometimes it will only recognize the up-ray. Sometimes it simply won’t recognize them at all. Moving from scene to scene sometimes helps, but as of late it has stopped working. The player is still connecting with the collider, but something in the physics 2d Raycast is not functional.

What is going on? This is really quite disheartening.

The addition of scenes, in itself, would not affect raycasting in any way. You need to debug this. The way I’d start by making public strings to view in the inspector what is actually being hit by the raycasts. So, for each of your raycasts add a public string, if the raycasthit is not null then fill the value with the hit game object’s name. Could be the origin of the ray is inside of the player’s collider.

Next step would be to use the Debug.DrawLine method to draw your rays, change their color when they hit something, and reduce the endpoint to the raycast hit point.