- Home /
Make the bullet in a 2d Top Down shooter get destroyed after passing mouse position
Hello everyone!
I'm making a 2d top down shooter game and I want the bullet to be destroyed after passing the mouse position that's set after clicking the mouse to shoot(I want to simulate the player shooting at that point not just in the direction of the mouse), but I don't know how could I accomplish this.
Note: The bullets are created as objects and not raycasted and they have a random spread so they won't always be going exactly through the mouse position Thanks in advance! :)
Answer by Casiell · Sep 16, 2018 at 09:07 PM
With random spread you would probably have to calculate the distance. Luckily there is a Vector3.Distance method you can use. Just take the distance from the mouse click and player, feed it to your bullet script and then check if your bullet is at greater distance than what you gave it. If it is, destroy it
Answer by hexagonius · Sep 16, 2018 at 09:09 PM
You can use Vector3.Project if I'm not mistaken.
vector is shoot origin towards the mouse and onNormal is the shoot direction. If the lenght of the resulting vector is shorter than the length of the traveled shoot direction, the bullet has bypassed the mouse as in orthogonally bypassed and you can destroy it.
Your answer
Follow this Question
Related Questions
Unity 2D game: all colliders not working 3 Answers
Ignore collision at the start of the game. 1 Answer
Unity 2D Collider did not create any collision shapes as they all failed verification 0 Answers
How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 1 Answer
Detect onMouseDown() on children colliders of a rigidbody parent 2 Answers