Really odd Raycast behavior, please help!

Ok, Since I decided it would be hard for me to explain exactly what it’s doing, I made a video: Dropbox - Error - Simplify your life

And here is the fire code for the rocket launcher (this actually happens with a shotgun prototype I’m working on too.)

It’s almost as if the ray freaks the heck out when I get close to an object. Because at range, it never seems to happen and works exactly as it should.

`

        Transform spawnPoint = GameObject.Find("spawn_point").transform;
        Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F,0.5F,0F));
        RaycastHit hit;
        Vector3 dir;
        if (Physics.Raycast(ray, out hit, Mathf.Infinity)){
            dir = (hit.point - spawnPoint.position).normalized;
        }
        else {
            dir = spawnPoint.forward;
        }
        var rot = Quaternion.FromToRotation(rocketProj.transform.forward, dir);
        GameObject rocket = Instantiate(rocketProj, spawnPoint.position, rot) as GameObject;
        rocket.rigidbody.velocity = dir * 60;

`

My guess is that you’re having a problem where the rocket isn’t colliding with the edge of an object. Is this the issue? If this is it, then it probably just has something to do with how fast the rocket is moving. As I understand it, Unity calculates collider physics collisions per-frame, so if one frame has the rocket on one side of the object, and the next frame has it on the other side (that is, if it moves in such large increments that it’s not actually ever “inside” the collider of the box) then your problem is just that you’re trying to figure out where to blow up the rocket in the wrong way. You can sort of see this happening in your video. The rocket is moving too quickly to register a collision with the box.

Maybe you don’t want to blow up the rocket when it actually hits an object using its colliders – this depends too much on how fast the engine is running, blah blah. Maybe, if it really has to go that fast, the rocket should raycast forward frame or so, and detect what it will be hitting. If that object is a certain distance away (either behind or in front of it), then we might say that it has “hit something”. That’s when you should detonate the rocket. But that leads us to another question – why does your rocket move so fast? If it does, then what’s the point of having an instant-damage weapon like a shotgun in the game? Seems like some functionality is crossing over…

I actually figured out my issue after reading over the above, and you are right, i switched the shotgun to a raycast weapon. The above problem is that I’m spawning the rocket projectile from my “fake” gun on the right of the screen and it’s traveling in a straight line to the hit.point. It’s actually a strange collision/graphical issue, i actually fixed it by shooting the projectile from the gun so it looks real and moving it over just a bit over time (player can’t tell it’s moved to the center of the screen).