- Home /
Gun script not working
I have this c# script that uses raycasting to shoot an enemy,. The enemy has an enemy tag and everything but it won't work and i'm not sure why. Can anyone help me figure this out?
using System.Collections;
using UnityEngine;
public class Pistol : MonoBehaviour
{
public int damage = 50;
public int ammo = 20;
public float range = 50;
private Transform mainCamera;
private AudioSource myAudioScource;
public AudioClip shootSound;
// Use this for initialization
void Start ()
{
myAudioScource = GetComponent<AudioSource>();
mainCamera = GameObject.FindGameObjectWithTag("MainCamera").transform;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Fire1")&& ammo > 0)
{
Shoot();
}
}
void Shoot()
{
Ray ray = new Ray(mainCamera.position, mainCamera.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, range))
{
if (hit.collider.CompareTag("Enemy"))
{
Debug.Log("Enemy HIT");
}
}
myAudioScource.PlayOneShot(shootSound);
ammo--;
}
}
Answer by Bleakmountain50 · Dec 01, 2018 at 05:41 AM
replace:
if (hit.collider.CompareTag("Enemy"))
{
Debug.Log("Enemy HIT");
}
with:
var target = hit.collider.GetComponent<INSERTENEMYSCRIPTHERE>();
if (target != null)
{
Debug.Log("Enemy HIT");
}
I just tried it, but no luck. Here is how it looks now. Did i add that in wrong maybe?
void Shoot()
{
Ray ray = new Ray(mainCamera.position, mainCamera.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, range))
{
var target = hit.collider.GetComponent<Enemy_Health>();
if (target != null)
{
Debug.Log("Enemy HIT");
}
}
myAudioScource.PlayOneShot(shootSound);
ammo--;
}
}
$$anonymous$$aybe changing var target = hit.collider.GetComponent<Enemy_Health>();
to var target = hit.transform.GetComponent<Enemy_Health>();
Answer by bburtson09 · Dec 05, 2018 at 05:41 AM
Does the enemy have a collider if so, is it a trigger? If it is a trigger pass an additonal param to Call to Physics.Raycast(...) " queryTriggerInteraction --> Specifies whether this query should hit Triggers."
Answer by alexrnorton89 · Dec 05, 2018 at 08:49 AM
Here's a part of the raycast based pseudo-voxel system I've been working on lately, and it works well so far. Instead of using CompareTag, I just use an == comparison to the tag itself. If that doesn't work, maybe your range is too short? Also, using GetComponent is resource intensive, while comparing tags is the least resource intensive way to identify a GameObject.
void PlaceCube()
{
RaycastHit hit;
//Debug.DrawRay(transform.position, Vector3.down, Color.green, 1000.0f);
if (Physics.Raycast(transform.position, Vector3.down, out hit, 1.0f))
{
if (hit.collider.tag == "Cube")
{
cubeBelow = hit.collider.gameObject;
//Stores the cube below into the raycasting cube for later access.
hit.collider.GetComponent<PCubeController>().SetCubeAboveInTheCubeBelow(ref thisCube);
//Passes the raycasting cube to the cube below as a reference for later access.
}
}
}
Your answer
Follow this Question
Related Questions
How do I get Single ammo reloading to work? 0 Answers
Total Bullets to weapon reload? Help 1 Answer
Limited Ammo? Help 1 Answer
FPS UI ammo display problem 0 Answers
Gun with limited ammo? 1 Answer