- Home /
Pretty simple problem: Bullet hole spawn wrong
I summon this function when the ray hits something. The prefab spawns at point 0,0,0 and not at hitpoint.
function BulletMark(){
var Hit : RaycastHit;
var hitRotation = Quaternion.FromToRotation(Vector3.up, Hit.normal);
Instantiate(bulletHole, Hit.point, hitRotation);
}
Answer by Griffo · Mar 23, 2013 at 03:16 PM
You need to get the ray hit point first, something like -
var rayCastHitPoint : Vector3;
var bulletHole : Transform;
private var hit : RaycastHit;
function Update () {
var ray : Ray = Camera.main.ViewportPointToRay (Vector3(0.5,0.5,0));
if (Physics.Raycast (ray, hit, 400)){
rayCastHitPoint = hit.point;
}
if(Input.GetKeyDown("Fire1")){
Instantiate(bulletHole, rayCastHitPoint, bulletHole.transform.rotation);
}
}
I told you, it spawns. It just doesn't spawn right. It spawns at the point 0,0,0. So most of it works.
Like @Griffo said, though, your Hit isn't initialized in your code, so your Hit.point will default to 0,0,0... so you need to set it with the Raycast above.
Glad to hear it, if my answer helped please mark it as answered, thank you.
Your answer
Follow this Question
Related Questions
How to Use Object Pooling? Is my Script Wrong? 0 Answers
Unity Crashes When Spawning Prefab 2 Answers
Need Help with Shooting Script c# 5 Answers
2D Sending Object From One Place To Mouse Position. 1 Answer