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);
}

1 Answer

1

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.

–

Oh, I see. I will try and do that.

–

Okay I got it working. Thanks.

–

Glad to hear it, if my answer helped please mark it as answered, thank you.

–