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