raycast 2D script issue

Hi there,

First time posting here. I am a 3D animator, and about a month ago I decided to start learning Unity and C#.

I am loving it so far. Extremely challenging and I am obviously still a complete noob. I hope that I am pasting this in the correct place.

I am busy following this tutorial and loving it - Making Your First Game: Unity 3D Zombie Shooter Platformer - Creating a 3D Obstacle - YouTube

All is going great but I am trying to do it in 2D. All has worked perfectly well up until now.

Seeing RayCast, RayCastHit and Physics are called in the bullet script. They do not collide with my 2D objects that are in the ‘Shootable’ layer. (Because they are using 2D colliders and 2D rigidbody’s)

Seeing I don’t really know what I am doing, I thought I could change the Raycast, RayCastHit and Physics to their 2D equivalents (IE Raycast2D, etc). Turns out I was well wrong. I get the error “cannot convert from unityengine.ray2D to unityengine.vector2”

Here is the script in question, apologies for the long post. Thank you very much for your time. I want to get this to work with my 2D colliders and I assume ‘2D physics’ but no clue what to do.

public class shootBullet : MonoBehaviour {

public float range = 10f;
public float damage = 5f;

Ray shootRay;
RaycastHit shootHit;
int shootableMask;
LineRenderer gunLine;

// Use this for initialization
void Awake () {
    shootableMask = LayerMask.GetMask("Shootable");
    gunLine = GetComponent<LineRenderer>();

    shootRay.origin = transform.position;
    shootRay.direction = transform.forward;
    gunLine.SetPosition(0, transform.position);

    if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
    {
        //hit an Enemy goes here
        gunLine.SetPosition(1, shootHit.point);
    }
    else gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
}

// Update is called once per frame
void Update () {
	
}

}

Much thanks,

Damian

If I change the raycasting to 2D and the 17th line to the following, the errors go away but still doesn’t work.

    if (Physics2D.Raycast(shootRay.origin, shootRay.direction, range, shootableMask)) 

Now if I shoot in the direction of an object that is on the Shootable layer, the gun shoots but I can see the line renderer goes off at a weird angle behind the character.

Completely stumped, any help would be greatly appreciated!

Thank you!

Damian