Search Unity

Bullet going through walls

Discussion in 'Scripting' started by denissuu, Mar 1, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there everyone!I'm currently making a Horror game, and I wanted to make a taser that you can shoot.First, I have used just raycasts and made it so when it hits the enemy, he plays an animation and then his other scripts get disabled.

    Now, I have changed my mind and I want to do some sort of ammunition and shooting system similar to how Granny works, but my take on it.Currently I'm only at the part where I instantiate the bullet itself, and then I'll also need to figure out how to make it stick to the target if it hits it for a couple of seconds.

    This is what I tried to implement inside my shooting script, and currently when I shoot into walls it just goes through them and that's pretty bad.I also wanted to make it so if it hits nothing, the taser bullet thing should still be instantiated in that way, but currently if I hit the air, nothing gets instantiated.


    Here is my try on doing these things :



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Taser : MonoBehaviour
    6. {
    7.     public float interactdistance = 60f;
    8.     public GameObject useposition;
    9.     public GameObject TaserAmmunition;
    10.     public GameObject bulletSpawn;
    11.     public Rigidbody taserAmmunition;
    12.     // Update is called once per frame
    13.     public void Shoot()
    14.     {
    15.         Ray ray = new Ray(useposition.transform.position, useposition.transform.forward);
    16.         RaycastHit hit;
    17.         if (Physics.Raycast(ray, out hit, interactdistance))
    18.         {
    19.             Rigidbody rigidbody = Object.Instantiate(taserAmmunition, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    20.             rigidbody.velocity = (hit.point - bulletSpawn.transform.position).normalized * 30f;
    21.             rigidbody.rotation = Quaternion.LookRotation(rigidbody.velocity);
    22.             Enemy enemy = hit.transform.GetComponent<Enemy>();
    23.             if (enemy != null)
    24.             {
    25.                 enemy.StunEnemy();
    26.             }
    27.         }
    28.  
    29.     }
    30.    
    31. }
    32.  

    If anyone could help me solve these issues I would be extremely grateful!

    Thanks for the help!You guys rock. <3
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    have u tried putting a collider on air or delay
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,565
    denissuu and Lethn like this.
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @denissuu

    Why the tag spamming in all your posts? Clearly this is not about all the topics you have tagged...
     
    denissuu and Yoreki like this.
  5. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Sorry for that, all of them seemed pretty relevant in my opinion.I'll make sure not to do that from now on.
     
  6. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    Thanks for the suggestion!I'll go ahead and try that.Do you have any sort of idea about what I could do so if I hit nothing the bullet still gets instantiated and in the proper way?
     
  7. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    Sorry, I don't think that I've understood this correctly.What do you exactly mean by putting a collider on the air?