Hi there everyone, my bullet holes are not in the correct position when I shoot, I tried so many things but I don’t know how to fix that. I tried to create a sphere (just to check the position) and the sphere is in the exact place! (but the bullet hole is not) how can we fix that? Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public Gun[] loadout;
public Transform weaponParent;
public GameObject bulletPrefab;
public LayerMask CanBeShot;
private int CurrentIndex;
private GameObject currentW;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
Equip(0);
if (currentW != null)
{
Transform tspawn = transform.Find("Gun");
Debug.DrawRay(tspawn.position, tspawn.forward,Color.red);
if (Input.GetMouseButtonDown(0))
Shoot();
}
}
void Equip(int ind)
{
if (currentW != null)
Destroy(currentW);
CurrentIndex = ind;
GameObject tnewEq = Instantiate(loadout[ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
tnewEq.transform.localPosition = Vector3.zero;
tnewEq.transform.localEulerAngles = Vector3.zero;
currentW = tnewEq;
}
void Shoot()
{
Transform tspawn = transform.Find("Gun");
RaycastHit thit = new RaycastHit();
if (Physics.Raycast(tspawn.position, tspawn.forward, out thit, 100f, CanBeShot))
{
GameObject tnewhole = Instantiate(bulletPrefab, thit.point + thit.normal * 0.001f, Quaternion.FromToRotation(Vector3.forward, thit.normal));
tnewhole.transform.LookAt(thit.point+thit.normal);
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = thit.point + thit.normal*0.001f;
Destroy(tnewhole, 15f);
}
}
}