Hey! I have a bullet prefab with that script attatched to it:
var point : Vector3;
var explosionRotation : Quaternion;
var explosion : GameObject;
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection(0, -10, 0);
if(Physics.Raycast(this.transform.position, fwd, hit, 2))
{
point = hit.point;
explosionRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Explode();
}
}
function Explode () {
Destroy(this.gameObject);
Instantiate(explosion, point, explosionRotation);
}
The problem is that sometimes projectiles go through stuff no matter how fast they are moving. What could the problem be and is that the right way to make weapon shoot in Unity?
Search the forum for ‘tunneling’ and ‘dontgothroughthings’ (and maybe ‘raycast’ and ‘projectile’ as well, possibly along with one or both of the other terms).
Alright, so I’ve been searching a long time. I found some threads with people, having the same problems. I found that “DontGoThroughThings” script and tried to use it with my projectile , but no effect. I’m using raycasting for my other weapons, that I’ve created so far, but this is some sort of alien blaster thing and I really need to use real projectiles with effects. Now, my projectile model is pretty big and most of the projectiles collide with things, but some of them just don’t do it correctly. I made a new layer and put them in it so I can see them no matter if they are behind something and I saw that some of the projectiles go through the surfaces and than change their direction really fast, like they have bounced of. From what I read, this is kinda fixed in Unity 3, but I’m not with Unity 3 and the only thing that I’ve found that should be fixing this problem is that script I mentioned and it doesn’t help at all
I can’t really attest to the effectiveness of the DontGoThroughThings script, as I haven’t used it myself.
If your projectiles are small enough that you can get away with it, I’d recommend using a raycast to represent their displacements for each update, as that’s liable to give you the most reliable results (I would imagine). Maybe others will be able to offer better suggestions though.
Yeah, on weapons like shotguns, rifles and pistols I’m using raycasting, but this is a plasma blaster or something that I need the projectiles for. It shoots big projectiles with materials and effects, so replacing this with a raycast wouldn’t be a good idea.
Hey, what if I do the following:
Put a character contrller on the bullet and check for collision. Would it be a good idea or I better not use this component for such things?
OK, so here’s what I found out. It doesn’t matter if my prjectiles’ speed is 200 or 700 or 1000, someof the projectiles just go through things if I shoot them in a speciffic angle the speeds I’m using are not enough for the projectiles to get miscalculated and not collide. For example I’m shooting at a terrain and most of the projectiles hit the terrain, but from time to time, some projectiles just clip through it shoot them at speciffic places from a speciffic position and angle.
Unless I am AGAIN mistaken, your raycast has a length of 2. If your game object next moves is way much more long than 2, the raycast is useless since it do not cover all the trajectory.
Oh, well I drew a debug ray from the projectile and even made it’s lenght far more than the projectile’s size. Maybe it doesn’t clip through the walls now, but if I shoot too close to a wall or the ground, the projectile will directly get destroyed, because it’s ray will detect the collider.
That’s a nice idea, but the projectiles still clip through the wall. As a matter of fact, they do it more often now and they do it even if I use a really low speed value like 5 or something.
By the way I’m using a Constant force component for the projectile, I’m not like giving the projectile a boost with a script if that matters…
I tried without it after I saw it’s not working. And I have the feeling it’s the constant force thing, but I managed to make my weapon shoot exactly where the crosshair is and if I use a script to modify the velocity of the projectile, the weapon becomes inaccurate, but I think projectiles don’t clip than. So does somebody know something about constant force that could probably be the problem
var ray = Physics.Raycast(transform.position, dir, hit, constantForce.relativeForce.z * Time.deltaTime, 9);
Debug.DrawRay (transform.position, dir* constantForce.relativeForce.z * Time.deltaTime, Color.green);
Yeah, now the projectiles won’t clip, because the ray is huge. So if it’s too large, the projectiles don’t clip I guess, but if I fire a projectile to a near distance it explodes immediately and if the ray is smaller, the projectiles clip.
It looks like you’ve already figured this out, but just because an object has a visual effect associated with it doesn’t mean you can’t use raycasts. Rather, the determining factor is really whether you can get away with representing the projectile as a point for the purpose of collision detection.
Maybe what’s happening: (don’t know that much about raycasting or colliders but this sounds feasible)
-Raycast not close enough to object to register hit
-Object moves beyond length of raycast to the inside of the object it was travelling towards
-new raycast is created INSIDE of new object
-raycast registers hit on opposite side of object that it is inside of, completely confusing you!