Search code examples
c#unity-game-engine2d-gamesraytracing

Unity C# Shooting Script with Raycast


I am creating a 2d over head game in Unity. I have so far been able to get the player to follow the mouse at a nearly correct angle (however it is still kind of moving around in an odd way). I am also trying to give the player a shooting function that shoots a ray from the correct angle (straight from the top of the player at the time of shooting). When I click using this code, nothing happens. I have objects set up for the player, bullet and fire point.

public void Update()
    {


        if (FollowMouse || Input.GetMouseButton(0))
        {
            _target = Camera.ScreenToWorldPoint(Input.mousePosition);
            _target.z = 0;
        }

        var delta = currentSpeed * Time.deltaTime;

        if (ShipAccelerates)
        {
            delta *= Vector3.Distance(transform.position, _target);
        }

        angle = Mathf.Atan2(_target.y, _target.x) * Mathf.Rad2Deg;
        transform.position = Vector3.MoveTowards(transform.position, _target, delta);
        transform.rotation = Quaternion.Euler(0, 0, angle);

        if ((Input.GetMouseButtonDown(0) || Input.GetKeyDown("space")) && Time.time > nextFire && numOfBullets > 0)
        {
            nextFire = Time.time + fireRate;
          //  Instantiate(bullet, firePoint.position, firePoint.rotation);
            numOfBullets--;
            // bullet.transform.position = Vector3.MoveTowards(bullet.transform.position, _target, bulletDelta);
            shoot();
            firePoint.position = _target;
        }

        if(Input.GetMouseButtonDown(1) && fuel > 0)
        {
            currentSpeed = zoomSpeed;
            while(fuel > 0)
            {
                fuel--;
            }
            currentSpeed = ShipSpeed;
        }


    }
    void Start()
    {
        currentSpeed = ShipSpeed;
    }

    void shoot()
    {
        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, 100, notToHit);
        Debug.DrawLine(firePointPosition, _target);
    }

Solution

  • Try This, it will shoot a raystarting from the position the gameobject is and go in the direction transform.right with a distance of 100 and ignore "notToHit". The debug.drawRay will show a red line in the scene view that shows the ray (with a distance of 1). Remove this after you have got everything working because it slows your game down.

    RaycastHit2D hit = Physics2D.Raycast(gameObject.transform.position,transform.right,100, notToHit);
    if (hit.transform != null) {
        Debug.Log ("You Hit: "hit.transform.gameObject.name);
    }
    Debug.DrawRay (gameObject.transform.position, transform.right, Color.red, 5);
    

    The reson I have used transform.right instead of calcluating the angle to the mouse position is because you say the player follows the mouse (so I assume the player is already looking at the mouse). However, if this isn't what you want you can always change it to how you want.