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

Shooting and damage from the shooting

Discussion in '2D' started by Zurvivor0412, Nov 10, 2016.

  1. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Weapon : MonoBehaviour {
    6.  
    7.     public float fireRate = 0;
    8.     public float Damage = 10;
    9.     public LayerMask whatToHit;
    10.     public GameObject playerObject;
    11.     private bool m_FacingRight = true;
    12.     public Transform BulletTrailPrefab;
    13.     public Transform MuzzleFlashprefab;
    14.     float timetoSpawnEffect = 0;
    15.     public float effectSpawnRate = 10;
    16.     private float move;
    17.  
    18.     float timeToFire = 0;
    19.     public Transform firePoint;
    20.  
    21.     // Use this for initialization
    22.     void Awake() {
    23.         Transform firePoint = transform.FindChild("firePoint");
    24.         if (firePoint == null) {
    25.             Debug.LogError("No FirePoint? what?!?");
    26.  
    27.             {
    28.             }
    29.  
    30.         }
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update() {
    35.         if (fireRate == 0) {
    36.             if (Input.GetButtonDown("Fire1")) {
    37.                 Shoot();
    38.             }
    39.         }
    40.         else {
    41.             if (Input.GetButton("Fire1") && Time.time > timeToFire) {
    42.                 timeToFire = Time.time + 1 / fireRate;
    43.                 Shoot();
    44.                 if (move < 0)
    45.                 {
    46.                     m_FacingRight = false;
    47.                     transform.localRotation = Quaternion.Euler(0, 180, 0);
    48.                 }
    49.                 else
    50.                 {
    51.                     m_FacingRight = true;
    52.                     transform.localRotation = Quaternion.Euler(0, 0, 0);
    53. }
    54.             }
    55.         }
    56.     }
    57.     private void Shoot()
    58.     {
    59.         Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    60.         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
    61.         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, mousePosition - firePointPosition, 100, whatToHit);
    62.         if (Time.time >= timetoSpawnEffect)
    63.             {
    64.             Effect();
    65.             timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
    66.         }
    67.         Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition) * 100, Color.cyan);
    68.         if (hit.collider != null)
    69.         {
    70.             Debug.DrawLine(firePointPosition, hit.point, Color.red);
    71.             Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
    72.         }
    73.     }
    74.     private void Effect() {
    75.         Instantiate(BulletTrailPrefab, firePoint.position, firePoint.rotation);
    76.         Transform Clone = Instantiate (MuzzleFlashprefab, firePoint.position, firePoint.rotation) as Transform;
    77.         Clone.parent = firePoint;
    78.         float size = UnityEngine.Random.Range(0.7f, 0.9f);
    79.         Clone.localScale = new Vector3 (size, size, size);
    80.         Destroy (Clone.gameObject, 0.2f);
    81.     }
    82. }
    83.  
    So it all works but it rarely does damage to actually what my mouse and the raycast is pointing at.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    See if this makes things a little clearer as to whats happening:

    Code (CSharp):
    1. private void Shoot() {
    2.     Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    3.     Vector2 shootDirection = (mousePosition - (Vector2)firePoint.position).normalized;
    4.     Ray2D ray = new Ray2D(firePoint.position, shootDirection);
    5.  
    6.     RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, 100, whatToHit);
    7.  
    8.     if(hit.collider != null) {
    9.         Debug.DrawLine(ray.origin, hit.point, Color.green);
    10.         Debug.Log("We hit " + hit.collider.name + " and did " + Damage + " damage.");
    11.     } else {
    12.         Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
    13.     }
    14.  
    15.     if(Time.time >= timetoSpawnEffect) {
    16.         Effect();
    17.         timetoSpawnEffect = Time.time + 1 / effectSpawnRate;
    18.     }
    19. }
     
  3. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    What ever way I face, and say i'm shooting at an enemy it doesn't matter what way i'm facing as it still damages the enemy.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Where is the code that damages the enemy?
     
  5. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    It just tells you what the damage is whatever sprite your hitting, e.g. building or an enemy at the moment.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Oh so what you're saying is that it always outputs that you're hitting the enemy even when you click elsewhere? What do you see from the Debug Drawline? Does it go towards the mouse and/or turn green? (assuming you're using that debug code i wrote)

    Perhaps try using if(hit) instead of if(hit.collider != null), it's possible that always comes back not null.
     
  7. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    The Debug Drawline is the opposite to where I face it with my cursor and only if its quick and easy to fix, how can I fix this? http://prntscr.com/d62lby
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Lets handle one thing at a time.

    You are using my code, correct? The DrawRay and DrawLine are using the same origin and direction, so I don't know why it would be any different.

    Either way, your shot appears to be going the correct direction.

    Do all the shots hit the same object or are they different?
     
  9. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    They hit different objects.
     
  10. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    I'm a little confused, just earlier you said that it always hits the enemy:
    It should be outputting whatever the raycast is hitting, even if the raycast is going backwards. The DrawLine function should be rendering exactly where the raycast is going. Is it outputting what the line is hitting?
     
  11. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    I used an enemy as an example.
     
  12. Zurvivor0412

    Zurvivor0412

    Joined:
    Aug 7, 2016
    Posts:
    36
    I'm as well watching Brackeys tutorials.