Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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

Discussion in 'Physics' started by San_Holo, Dec 20, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    135
    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

    Code (csharp):
    1. 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
    2.         Debug.DrawLine (transform.position, new Vector3 (middle.transform.position.x, middle.transform.position.y + 4f, middle.transform.position.z));
    3.                 if (hit.collider != null) {
    please advise me, thank you
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,150
    The 2D and 3D physics engines are entirely separate and can't interact.

    --Eric
     
    San_Holo likes this.
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    135
    Thanks, I got it working...

    Code (csharp):
    1.         private RaycastHit hit;
    2.         int layerMask = 1 << 9;
    Layer nine is my enemy layer,

    Code (csharp):
    1. float maxLaserSize = 1000f;
    2.                 float currentLaserSize = maxLaserSize;
    3.                 Vector3 laserDirection = this.transform.up;
    4.  
    5.                 if (Physics.Raycast (this.transform.position, laserDirection, out hit, Mathf.Infinity, layerMask)) {
    6.                         print ("raycast hit:" + hit.transform.name);
    etc etc etc