Instantiate explosion on fast moving object
I know the basics of instantiating a prefab. My code works in that when the rocket hits a wall , an explosion is instantiated. The problem is that my rocket is moving so fast that the explosion seems to "lag" and rather than the explosion taking place on the surface of the object it hits, it explodes more inside the object. So I am thinking what I want to do is OnTriggerEnter, get the EXACT location of where the rocket made contact ( a Vector3?) and instantiate the explosion there rather than the rocket location which is several feet further along its path. But I can't seem to get the explosion to instantiate at a Vector3 location. Any ideas?
I mean I need to somehow mark the position of where the rocket makes initial contact with target and instantiate explosion on that mark rather than on the rocket object. Either that or slow the rocket down - which works but doesn't really feel or look right
Answer by streeetwalker · Apr 29, 2020 at 06:34 PM
Hi @shadowpuppet, you can the rocket collider's method, ClosestPointOnBounds(transform.position) in OnTriggerEnter to get a point. See the unity documentation on that.
It seems you have another problem - you can't instantiate the explosion? We'd need to see your setup and code.
no, I can instantiate the explosion just fine, it's just that the rocket speed is 4500 and the explosion that gets instantiated in rockets position a frame later is beyond the point of impact. It explodes , but in the case of ...a wall, for instance, rather than see the explosion effects on the front of the wall they take place on the other side of the wall. An easier fix was I simply lowered the rocket speed to 1500. visually the rocket seems to travel nearly as fast - or at least fast enough to feel like a rocket - and explodes on impact much closer to front side of wall. I had the rocket speed cranked up so high for use in a fast moving vehicle so the rocket needed to have that extra boost or it would travel just barely faster than the moving vehicle from which it was fired. I have since disabled that so rockets only come from a person walking so was able to dial down the velocity of the rocket to where it works and explodes as desired - at the point of impact and not 4 feet further down the rockets path behind the target
@shadowpuppet, I know what you mean. You can also try setting a different collision detection method on your Rigidbody - there are several in the inspector and one of them might do a better job as well in combination with that Bounds code.
An alternative method is to do a RayCast along the z axis of your rocket - assu$$anonymous$$g z is forward, to deter$$anonymous$$e where it hit's the edge of the collider - you can get the hit point from that.
This is a common problem and you'll find quite a few discussions about it.
Answer by shadowpuppet · Apr 29, 2020 at 09:05 PM
@streetwalker. I like the raycast idea as can tweak the distance cast with rocket speed so could detect the wall a bit early and explode as hits wall/object. This is an old project that I have fiddled with off and on for a few years. Virus lock down has me picking it up again and debugging. The rocket thing was annoying because all the cool visuals were behind a wall. I'm going to play with the raycast . Thanks
Your answer
Follow this Question
Related Questions
Only allowing one instance of prefab created by button press & destroying prefab on collision? 0 Answers
How to instantiate a prefab using a string variable ? 1 Answer
null reference on instantiated prefab array at instantiation 1 Answer
instantiate same object multiple time at different positions unity2d 0 Answers
Problems with instantiated objects in a circle formation 0 Answers