Prefab is instantiating in the corner -How do I fix?

Hey guys,

I am trying to spawn a prefab from capsule cast… Can you tell me why its spawning in the corner of the map?

The script is:

var boom : GameObject;

function Update () {
    var hit : RaycastHit;
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var charContr : CharacterController = GetComponent(CharacterController);
    var p1 : Vector3 = transform.position + charContr.center + 
        Vector3.up * (-charContr.height*0.5);
    var p2 : Vector3 = p1 + Vector3.up * charContr.height;

    if (Input.GetButtonDown ("Fire1")) {
        Physics.CapsuleCast (p1, p2, charContr.radius, ray.direction, hit, 10);
            Instantiate (boom, hit.point, transform.rotation);
        }
    }

The problem is that it instantiates the prefab in the corner of the map…

All Help Is Appreciated~

-Thanks

What, nobody has any ideas?

Please Speak Up If You Do!

Have you done a print (hit.point) to debug it or Debug.Log? Sounds like it’s intantiating at the terrain (0,0,0) Your hit.point isn’t registering, which is probably because you have two raycasts going on. You’re redefining the hit variable as (0,0,0) in the capsule cast (this is my theory anyway). Not quite sure why you’re using the capsule cast when you want “boom” to instantiate at the mouse position. At least that’s what I’m assuming you want to do. You might try commenting out the capsule cast and see what happens. But first I’d want to know what the hit.point value is. As I said, I suspect it’s always zero the way you’ve set it up.

You should wrap your Physics.CapsuleCast in an if statement, so that it only Instantiates an object if there is a hit.

if (Input.GetButtonDown ("Fire1")) {
    if (Physics.CapsuleCast (p1, p2, charContr.radius, ray.direction, hit, 10))
        Instantiate (boom, hit.point, transform.rotation);
}

How do you suggest I fix, or test, it?

Thank you! It works! I just had to change the range…