Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Bullets passing through platforms - Brackeys Tutorial

Discussion in '2D' started by kuyakyleee, Jan 15, 2021.

  1. kuyakyleee

    kuyakyleee

    Joined:
    Jan 15, 2021
    Posts:
    2
    I'm following the tutorial for 2D Platformer tutorial and currently at video 17. Wherein Brackeys changed how the bullet trail works and added hitEffect when hitting objects.

    When shooting at the enemies it works but when I tried shooting the platforms it just passes through. I tried following and recreating the script many times but just couldn't make it work. I'm thinking there's something wrong with the platform themselves?

    I'm new to unity and game development itself and would appreciate any help.

    Here is a photo of what's happening



    Or maybe I overlooked some things on the Weapon script used:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Weapon : MonoBehaviour {
    5.  
    6.     public float fireRate = 0;
    7.     public int Damage = 10;
    8.     public LayerMask whatToHit;
    9.    
    10.     public Transform BulletTrailPrefab;
    11.     public Transform HitPrefab;
    12.     public Transform MuzzleFlashPrefab;
    13.     float timeToSpawnEffect = 0;
    14.     public float effectSpawnRate = 10;
    15.  
    16.    
    17.     float timeToFire = 0;
    18.     Transform firePoint;
    19.  
    20.     // Use this for initialization
    21.     void Awake () {
    22.         firePoint = transform.Find ("FirePoint");
    23.         if (firePoint == null) {
    24.             Debug.LogError ("No firePoint? WHAT?!");
    25.         }
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.         if (fireRate == 0) {
    31.             if (Input.GetButtonDown ("Fire1")) {
    32.                 Shoot();
    33.             }
    34.         }
    35.         else {
    36.             if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
    37.                 timeToFire = Time.time + 1/fireRate;
    38.                 Shoot();
    39.             }
    40.         }
    41.     }
    42.    
    43.     void Shoot () {
    44.         Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    45.         Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
    46.         RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
    47.  
    48.         Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
    49.         if (hit.collider != null) {
    50.             Debug.DrawLine (firePointPosition, hit.point, Color.red);
    51.             Enemy enemy = hit.collider.GetComponent<Enemy>();
    52.             if (enemy != null) {
    53.                 enemy.DamageEnemy (Damage);
    54.                 Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
    55.             }
    56.         }
    57.  
    58.         if (Time.time >= timeToSpawnEffect)
    59.         {
    60.             Vector3 hitPos;
    61.             Vector3 hitNormal;
    62.  
    63.             if (hit.collider == null) {
    64.                 hitPos = (mousePosition - firePointPosition) * 30;
    65.                 hitNormal = new Vector3(9999, 9999, 9999);
    66.             }
    67.             else
    68.             {
    69.                 hitPos = hit.point;
    70.                 hitNormal = hit.normal;
    71.             }
    72.  
    73.             Effect(hitPos, hitNormal);
    74.             timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
    75.         }
    76.     }
    77.  
    78.     void Effect(Vector3 hitPos, Vector3 hitNormal)
    79.     {
    80.         Transform trail = Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation) as Transform;
    81.         LineRenderer lr = trail.GetComponent<LineRenderer>();
    82.  
    83.         if (lr != null)
    84.         {
    85.             lr.SetPosition(0, firePoint.position);
    86.             lr.SetPosition(1, hitPos);
    87.         }
    88.  
    89.         //Destroy(trail.gameObject, 0.04f);
    90.  
    91.         if (hitNormal != new Vector3(9999, 9999, 9999))
    92.         {
    93.             Transform hitParticle = Instantiate(HitPrefab, hitPos, Quaternion.FromToRotation (Vector3.right, hitNormal)) as Transform;
    94.             //Destroy(hitParticle.gameObject, 1f);
    95.         }
    96.  
    97.         Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
    98.         clone.parent = firePoint;
    99.         float size = Random.Range (0.6f, 0.9f);
    100.         clone.localScale = new Vector3 (size, size, size);
    101.         //Destroy (clone.gameObject, 0.02f);
    102.  
    103.     }
    104. }
    105.  
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    763
    You are shooting a raycast and it will only hit what is determined by the LayerMask variable, WhatToHit you have set up. Also, your platforms may only have a collider on the top-most part and not on the skinny vertical part so even if you set up their layers properly, it wouldnt collide. Basically, see what the layer is on the enemy and you can use that for your platforms too. There are other ways but this is the simplest to show you how it works.
     
  3. kuyakyleee

    kuyakyleee

    Joined:
    Jan 15, 2021
    Posts:
    2
    Dang, how could I overlook such simple thing. I got so flustered trying to figure out what's wrong. The platforms wasn't checked out on my WhatToHit.
    Thank you so much for your time!
     
    Cornysam likes this.
unityunity