2D Raycasts Help!

Updated on March 28, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on January 5, 2018

I’m attempting to make a 2D shooter where you are a stationary launcher on the ground and you shoot up at asteroids falling to the Earth, then destroy them. I’m getting caught up on collision detecting. All of the assets used are on the same z axis, except for the background, if that helps. I was following one of Brackey’s videos, and I think I missed something. I re watched them, but can’t figure it out. Here’s my code that is applied to the launcher;

 

 
public float timeToFire;
 public float fireRate;
public LayerMask toHit;
public GameObject bullet;
public Transform firePoint;
void Start () {
 }
 
//Dectecting mouse button
 void Update () {
     if (fireRate == 0)
     {
          if (Input.GetButtonDown("Fire1"))
          {
          Shoot();
          }
     }
     else
     {
          if (Input.GetButton("Fire1") && Time.time > timeToFire)
          {     
          timeToFire = Time.time + 1 / fireRate;
          Shoot();
          }
     }
 }
 
// ABOVE CODE IS WORKING
 
void Shoot()
 {
        Debug.Log("Fired"); //WORKS
 
     //UNCLEAR IF WORKING
 Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
        Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
       RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition - firePointPosition, 99999999, toHit);
        Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition)*999999);
//BELOW DEFINATLY DOESN'T WORK
        if(hit.collider != null)
        {
        Debug.DrawLine(firePointPosition, hit.point, Color.red);
        Debug.Log("Hit");
        }
    }
}
 

The draw lines will obviously eventually be replaced with actual bullets

Any errors in spacing at the beginning of lines is due to the forums, not the code.

 

Thanks for the help everyone!.

  • Liked by
Reply
2 on January 5, 2018

Just change it a little bit like this

RaycastHit2D hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
if (Physics2D.Raycast(ray, out hit))
{
//your code  
}

You have to change the ray to your likings. So that you create a ray from your firePoint to the mousePoint.

on January 5, 2018

It’s giving me errors on the words ray and hit in the if statement. “Cannot convert from ‘Unity.Ray’ to ‘Unity.Vector2′” and “may not be passed with the out keyword” respectively. The ray was already being made (as far as I know) , just wasn’t colliding with the asteroids falling from the sky. The debug draw line was showing up, so I assumed the ray was being made, just not detecting collisions.

on January 5, 2018

Here’s the new code;

 RaycastHit2D hit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 Debug.DrawLine(firePointPosition, mousePosition);
if(Physics2D.Raycast(ray, out hit)) //errors on this line
 {
     Debug.DrawLine(firePointPosition, hit.point, Color.red);
     Debug.Log("Hit");
 }
Show more replies
  • Liked by
Reply
Cancel
0 on March 28, 2018

I ended up just making a small bullet fire with some vector calculations instead. Thanks anyways!

 

  • Liked by
Reply
Cancel