Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Top down shooting script... Don't know what's going wrong

Discussion in 'Scripting' started by Brumakk, Jan 30, 2015.

  1. Brumakk

    Brumakk

    Joined:
    Jan 30, 2015
    Posts:
    2
    So I am making a simple top down shooter/battle arena game and I cant get my character to shoot a raycast. Debugging doesn't give me any errors.

    Also note that i want the ray to be shot in the direction that my character is facing. it doesnt follow the mouse.

    Here is my script. Is there anything here that I am missing?


    If i type
    [[Debug.LogError ("shoot")]] after shoot () then it gives me that error so i know my key bind is responsive. help would be awesome.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class weapon : MonoBehaviour {
    5.  
    6.     public float fireRate = 0;
    7.     public float damage = 10;
    8.     public LayerMask whatToHit;
    9.     public Transform firePoint;
    10.  
    11.     float timeToFire = 0;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Awake () {
    16.         firePoint = transform.FindChild ("FirePoint");
    17.         if (firePoint == null) {
    18.             Debug.LogError ("No firepoint? WHATT?");
    19.         }
    20.     }
    21.     // Update is called once per frame
    22.     void Update () {
    23.         if (fireRate == 0) {
    24.             if (Input.GetKeyDown (KeyCode.H))
    25.                 Shoot ();
    26.             }
    27.         {
    28.         {
    29.             if (Input.GetKey (KeyCode.H) && Time.time > timeToFire) {
    30.                     timeToFire = Time.time + 1/fireRate;
    31.                     Shoot();
    32.             }
    33.            }
    34.     }
    35.     }
    36.  
    37.     public void Shoot () {
    38.         Ray ray = new Ray (firePoint.position, firePoint.forward);
    39.         RaycastHit hit;
    40.  
    41.         float shotDistance = 100;
    42.  
    43.         if (Physics.Raycast (ray,out hit, shotDistance)) {
    44.             shotDistance = hit.distance;
    45.  
    46.         Debug.DrawRay(ray.origin,ray.direction * shotDistance,Color.red);
    47.         }
    48.     }
    49. }
    50.      
    51.  
     
    Last edited: Jan 30, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    How do you know it's not working?
    You don't actually have anything happen when the raycast hits something
    tip: add a number to your drawRay parameters for a duration, eg
    Debug.DrawRay(ray.origin,ray.direction * shotDistance,Color.red, 5f)
    And it will draw the ray for 5 seconds (instead of a single frame)

    I think you also need to check the number of brackets you've got
     
  3. Brumakk

    Brumakk

    Joined:
    Jan 30, 2015
    Posts:
    2
    Well I'm pretty sure it isn't working because I'm not seeing a ray. Isn't that something I should see when I shoot? Regardless whether it hits something?
    also I'm pretty new to this.