This is the code.
nothing happens when the ray is hitting the enemy.
i’m trying to make it spawn a new enemy but once i added the code it stopped working with no errors in unity or in visual studios.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun : MonoBehaviour
{
public Transform BulletPos;
public Animator GunAnimController;
public GameObject prefab;
private GameObject Clone;
public GameObject EnemyPrefab;
private void Start()
{
}
// Update is called once per frame
public AudioSource GunSound;
public float timeBetweenShots;
private float timestamp = 0f;
public Transform RandomEnemyPos;
private Vector3 RandomEnemyLoc;
void Update()
{
if (timestamp < Time.deltaTime && (Input.GetButtonDown("Fire1")))
{
GunAnimController.Play("GunAnimation");
RaycastHit TheHit;
if (Physics.Raycast(BulletPos.position, BulletPos.TransformDirection(Vector3.forward), out TheHit))
{
Debug.Log("hit");
if (TheHit.collider.gameObject.tag == "Enemy")
{
Destroy(TheHit.collider.gameObject);
RandomEnemyLoc = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
RandomEnemyPos.position = RandomEnemyLoc;
Instantiate(EnemyPrefab, RandomEnemyPos);
}
Clone = Instantiate(prefab, (TheHit.point), Quaternion.identity);
Destroy(Clone, 10);
}
GunSound.Play();
timestamp = Time.time + timeBetweenShots;
}
}
}