- Home /
Why does my bullet only bounce horizontally or vertically, even when it's fired at an angle
@MelvMay No matter what angle I fire my bullets at, they always bounce either horizontally or vertically. Why is this, and what can I do to fix it so the bullets bounce realistically? I've illustrated the problem in the image below, can someone help?
In what context are the bullets bouncing? Is this driven by physics? Is it a scripted bounce (i.e. OnCollisionEnter())? Without more information on what you're currently doing, it's impossible to help, despite the behavioral description provided by your image.
To add on to what Eno-Khaon asked, is it an object based bullet or a raycast based bullet?
@$$anonymous$$o-Khaon @TechnicPyro Sorry for the late reply, this is driven by the Unity physics engine (Rigidbody2D, Collider2D). It is an object, so collisions not raycasts. I do some checks and keep the bullet from spinning in OnCollisionEnter2D() and LateUpdate(), but I've tried it without those changes, and the problem still occurs.
As some follow-up questions, then (Not necessarily that I need the answer to, but things to consider, more than anything else):
What shape(s) of collider(s) are you using on the bullets? Are the walls they're hitting square colliders? (If not, are they convex or concave?) Are they hitting the intersection of two adjacent wall panels? What physics material properties do they have? Are there any other adjustments made other than orientation correction in scripts?
There are numerous little possibilities that could have an influence on their interaction when left entirely to the physics engine. If the bullets are flying especially quickly, it could be a hiccup in resolving their new velocity. Even if not, it's easy to wind up with unexpected results.
It doesn't necessarily explain the results you demonstrated with the image, but without having experienced that behavior myself, I'm just trying to help think of as many possibilities as come to $$anonymous$$d. Of course, if anything stands out, just let us know!
The bullets have triangular colliders (EdgeCollider2D), and the wall colliders are basically rectangular. I used the custom physics shape window and set the tolerance to lowest, so the colliders almost perfectly follow the shape of the sprite. I've attached a screenshot below. The problem is not that the bullets are hitting the intersection of different colliders, because I've tested multiple hit points and angles. The map and the bullets are two different physics materials: the map has a friction of 100 and a bounciness of 0.05, and the bullets have a friction of 0.4 and a bounciness of 0.9. And no, I don't make any other adjustments in scripts. It's all up to the physics engine.
One answer I thought of could be that the bullet is getting too far into the wall before it senses the collision, so it rebounds straight out of it. I will try lowering the fixed timestep, and seeing if that helps.
Thank you!
Answer by rubcc95 · Nov 05, 2020 at 10:19 PM
Are you using transform.position in order to move your bullet?
Think about use physics engine and some Physics material.
Your answer
Follow this Question
Related Questions
How to tell if a 2D object is in a certain area 2 Answers
Collision point problem 1 Answer
How to optimize a lot of colliders? 1 Answer
Tilemap collider doesn't appear 1 Answer
Unable to detect collision in 2d game please help!! 1 Answer