Search Unity

2D Shooting at mouse position help

Discussion in 'Scripting' started by lewis1682, Aug 20, 2014.

  1. lewis1682

    lewis1682

    Joined:
    Feb 18, 2014
    Posts:
    4
    Hi guys,

    So I am trying to make a 2D sidescroller,its been going well apart from now... I have tried to set up some shooting, to do this I have added a empty game object and named firePoint, this is the point that I want the laser trail prefab to spawn at, this works and it shoots off, however it doesn't shoot on the rotation of the mouse, however I am using some debugging techniques to draw a line, this works as I can see the line draw from the fire point and past the mouse on the right rotation so this is why I am confused as to why the laser prefab is not using the same angle, my code is below and I would be grateful for any suggestions or input.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shooter : MonoBehaviour {
    5.  
    6.     public float fireRate = 0;
    7.     public float damage = 10;
    8.     public LayerMask whatToHit;
    9.     public AudioClip laserSound;
    10.     public Transform laserTrailPrefab;
    11.  
    12.     private float timeToFire = 0;
    13.     Transform firePoint;
    14.  
    15.     // Use this for initialization
    16.     void Awake () {
    17.         firePoint = transform.FindChild ("FirePoint");
    18.         if (firePoint == null) {
    19.             Debug.LogError ("Fire Point is null");
    20.         }
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.         if (fireRate == 0) {
    26.                         if (Input.GetButtonDown ("Fire1")) {
    27.                                 Shoot ();
    28.                         }
    29.                 } else {
    30.                     if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    31.                 timeToFire = Time.time +1/fireRate;
    32.                 Shoot();
    33.             }
    34.                 }
    35.     }
    36.     void Shoot () {
    37.         Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    38.         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    39.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
    40.         Effect ();
    41.         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.red);
    42.  
    43.  
    44.         if (hit.collider != null) {
    45.             Debug.DrawLine (firePointPosition, hit.point, Color.green);  
    46.             Debug.Log ("We hit "+ hit.collider.name + " and did " + damage + " Damage.");
    47.         }
    48.     }
    49.  
    50.     void Effect() {
    51.         Instantiate (laserTrailPrefab, firePoint.position, firePoint.rotation);
    52.         audio.PlayOneShot (laserSound);
    53.     }
    54. }
    55.  
    Thank you
     
  2. Karwler

    Karwler

    Joined:
    Aug 25, 2014
    Posts:
    18
    Try that tutorial:

    It might help.