Search Unity

Problem with Bullet 2D lineRenderer

Discussion in 'Scripting' started by Jamesafk, May 25, 2015.

  1. Jamesafk

    Jamesafk

    Joined:
    May 22, 2015
    Posts:
    9
    want to shoot every time I give a click but the en gusts as if the line remains Gizmos ,

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Weapons : MonoBehaviour {
    5.  
    6.     public float fireRate = 0;
    7.     public float damage = 10;
    8.     public LayerMask whatToHit;
    9.  
    10.     public Transform BulletTrailPrefab;
    11.  
    12.     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 ("No firepoint? WHAT?!");
    20.         }
    21.     }
    22.  
    23.  
    24.     // Update is called once per frame
    25.     void Update () {
    26.         Shoot ();
    27.         if (fireRate == 0) {
    28.             if (Input.GetButtonDown ("Fire1")) {
    29.                 Shoot ();
    30.          }
    31.         }
    32.         else {
    33.             if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    34.                 timeToFire = Time.time + 1/fireRate;
    35.                 Shoot();
    36.           }
    37.    }
    38. }
    39.     void Shoot () {
    40.         Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
    41.         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    42.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
    43.         Effect ();
    44.         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
    45.         if (hit.collider != null) {
    46.             Debug.DrawLine (firePointPosition, hit.point, Color.red);
    47.             Debug.Log ("We hit " + hit.collider.name + " and did " + damage + " damage.");
    48.         }
    49.  
    50.         }
    51.     void Effect () {
    52.         Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
    53. }
    54. }
     

    Attached Files:

  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    455
    Because you have Shoot(); on line 26 as the first line of your Update function without telling it when to shoot, it's just shooting all the time, every frame... Take that out and see if you have any better luck. In other words, change your Update function to be something more like:

    Code (CSharp):
    1.     void Update () {
    2.         if (fireRate == 0) {
    3.             if (Input.GetButtonDown ("Fire1")) {
    4.                 Shoot ();
    5.             }
    6.         }
    7.         else {
    8.             if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    9.                 timeToFire = Time.time + 1/fireRate;
    10.                 Shoot();
    11.             }
    12.         }
    13.     }
    Not sure if that will completely solve your problem, but I guarantee it's a step in the right direction.
     
    Jamesafk likes this.
  3. Jamesafk

    Jamesafk

    Joined:
    May 22, 2015
    Posts:
    9
    you are the best dude haha! :cool: thx!!!
     
    krougeau likes this.