- Home /
How do you keep a shot moving in the same direction/velocity after it has hit a target while allowing targets to bounce off of obstacles?
I have a shot that gets fired from a turret which needs to be able to hit multiple targets (once it hits one target the target disappears and the shot should continue in the same direction with the same velocity and can continue on and hit others). My issue is that the targets I currently have are triggers which can run into obstacles, but since they are triggers they simply pass through these obstacles. If I remove the trigger then they correctly run into and bounce off of obstacles but then the shot bounces off of them after hitting them rather than continuing through. So my question is how do I allow shots to hit targets and continue through them after hit while allowing targets to hit and bounce off of obstacles?
Answer by coe720 · Nov 23, 2016 at 07:32 AM
Thanks for the answers you guys, that definitely helped me go in the right direction. I ended up using Physics2D.OverlapCircleAll with the origin point being the center of my shot which returns a list of 2D Colliders that the circle encounters. Then I check the tag of each game object in that list to see if it's an actual target as opposed to an obstacle.
Answer by NotEnoughLemons3D · Nov 22, 2016 at 07:18 PM
Check out Physics.RaycastAll
https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html
You can specify the distance of the Raycast and it basically sends out an imaginary line from a start point to a specified direction returning all of the objects it encounters. This way you can leave isTrigger toggled off and still detect all objects within the firing direction.
If you are using 2D you need to check out Physics2D's version.
https://docs.unity3d.com/ScriptReference/Physics2D.RaycastAll.html
Answer by hexagonius · Nov 22, 2016 at 09:57 PM
what you could try is using the following trick. save the current velocity during each FixedUpdate step in the shot. OnCollisionEnter, handle your logic, use Phusics.IgnoreCollision to explicitly ignore any further collision with bullet and target and reapply the saved velocity. this way the bullet keeps moving while the target should bounce off.
Your answer
Follow this Question
Related Questions
CircleCollider2D vs PolygonCollider2D: which collision solving is less expensive? 1 Answer
How to add colliders as objects into a public variable in the inspector? 1 Answer
overlayCircle not working 1 Answer
Detecting top most UI element using Collider2D 0 Answers
Physics2D.OverlapArea inaccurate? 2 Answers