Can I use 2D raycasts for my 3D stuff?

Hi…

I’m kinda converting this laser script to a vertical orientation and according to my debug line drawing its going in the direction i want it to go, but this script was for a 2D side scroller and my game is a vertical 3D affair, but nothing is firing, perhaps my alien-invader prefabs need a 2D box collider instead of their normal box collider?

So the snippet below is obviously snagging me some where, no errors at all, I’m just speaking the wrong language perhaps

RaycastHit2D hit = Physics2D.Raycast (transform.position, new Vector3 (middle.transform.position.x, middle.transform.position.y + 4f, middle.transform.position.z), 9); // layer '9' is enemies layer
		Debug.DrawLine (transform.position, new Vector3 (middle.transform.position.x, middle.transform.position.y + 4f, middle.transform.position.z));
				if (hit.collider != null) {

please advise me, thank you

The 2D and 3D physics engines are entirely separate and can’t interact.

–Eric

1 Like

Thanks, I got it working…

		private RaycastHit hit;
		int layerMask = 1 << 9;

Layer nine is my enemy layer,

float maxLaserSize = 1000f;
				float currentLaserSize = maxLaserSize;
				Vector3 laserDirection = this.transform.up;

				if (Physics.Raycast (this.transform.position, laserDirection, out hit, Mathf.Infinity, layerMask)) {
						print ("raycast hit:" + hit.transform.name);

etc etc etc