- Home /
Bullets holesnot instantiating in the correct position
I have been following a tutorial series by a youtube channel called Jimmy Vegas and after watching his video on bullet holes and how to make them appear where you fire I found that the bullet holes either instantiate inside the wall or are floating in mid-air. Any help would be great. Here is the code I have used: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PistolDamage : MonoBehaviour { public int pistolDamage = 5; public float TargetDistance; public float rangePistol = 15.0f; public GameObject gunObject; public GameObject zombie; public GameObject bullet; public RaycastHit hit; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
if (GunScript.reloading == false)
{
RaycastHit Shot;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < rangePistol)
{
Shot.transform.SendMessage("DeductPoints", pistolDamage, SendMessageOptions.DontRequireReceiver);
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
{
Instantiate(bullet,hit.point,Quaternion.LookRotation(hit.normal));
}
}
}
}
}
}
}
Why are you using a second raycast?, you could just use the first ones Shot.normal
? Also, to save on overhead you could just set the max range for the raycast to be that of the weapon being used and then its a case of if it hits or not, no check of the distance would be needed.
But to answer your question, put it in a FixedUpdate for collision detection.
Update runs once per frame. FixedUpdate can run once, zero, or several times per frame, depending on how many physics frames per second are set in the time settings, and how fast/slow the framerate is.
Thanks for the help. Sorry I still have a lot to learn with C#. Hopefully I can learn from them!
Your answer
![](https://koobas.hobune.stream/wayback/20220612195842im_/https://answers.unity.com/themes/thub/images/avi.jpg)