- Home /
bullet hole on shot
I have this script that shoots a bullet from my muzzlePoint to my cursor. If i want to add a bullet hole the bullet hole will be on the wall before the bullet hits. So i wanted to use the function OnTriggerEnter. How can i get this to work when it shoots?
#pragma strict
var bullet : Transform; // the bullet prefab
private var spawnPt : GameObject; // holds the spawn point object
var bulletHole : Transform;
function Start()
{
}
function Update()
{
if(Input.GetMouseButtonDown(0))
{
// only do anything when the button is pressed:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100))
{
if (!spawnPt) spawnPt = GameObject.Find("muzzlePoint");
var projectile = Instantiate(bullet, transform.position, Quaternion.identity);
projectile.transform.rotation = Quaternion.LookRotation(ray.direction);
projectile.rigidbody.AddForce(ray.direction * 1000);
}
OnTriggerEnter();
audio.Play();
}
}
function OnTriggerEnter (other : Collider) {
var hit : RaycastHit;
if (other.gameObject.tag == "wall") {
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(bulletHole, hit.point, hitRotation);
}
}
The usual way to do this is to fire a ray from the bullet each frame with a ray length related to how far the bullet with travel in the next frame, effectively it's velocity multiplied by deltaTime. If the ray passes through the wall, then you know the bullet is about to hit.
Not entirely sure what you're getting at, since there was a lot of ambiguous use of the English language there. $$anonymous$$y guess is you want a Bullet Hole to spawn when the bullet hits a wall, is this correct?
Yes this is correct. Excuse me for my English it is not my native language.
Your answer