Bullet going through walls

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 :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Taser : MonoBehaviour
{
    public float interactdistance = 60f;
    public GameObject useposition;
    public GameObject TaserAmmunition;
    public GameObject bulletSpawn;
    public Rigidbody taserAmmunition;
    // Update is called once per frame
    public void Shoot()
    {
        Ray ray = new Ray(useposition.transform.position, useposition.transform.forward);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, interactdistance))
        {
            Rigidbody rigidbody = Object.Instantiate(taserAmmunition, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
            rigidbody.velocity = (hit.point - bulletSpawn.transform.position).normalized * 30f;
            rigidbody.rotation = Quaternion.LookRotation(rigidbody.velocity);
            Enemy enemy = hit.transform.GetComponent<Enemy>();
            if (enemy != null)
            {
                enemy.StunEnemy();
            }
        }

    }
   
}

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

Thanks for the help!You guys rock. <3

have u tried putting a collider on air or delay

To prevent tunneling (high speed objects phasing through thin obstacles without triggering collision), you need to set
collision detection mode to Continuous or ContinuousDynamic.
This comes with performance cost.

Default collision mode is discrete, I believe. Which allows tunneling.

2 Likes

@denissuu

Why the tag spamming in all your posts? Clearly this is not about all the topics you have tagged…

2 Likes

Sorry for that, all of them seemed pretty relevant in my opinion.I’ll make sure not to do that from now on.

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?

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