- Home /
The question is self answered
Why are the reflected bullets in my 2D game acting strange?
I'm making a top down 2D space shooter, my first experiment with Unity physics scripting.
The game level is surrounded by walls, and I'm using a raycast to check collision with the wall layer, to reflect billiard physics style. This works fine for the player controller script, and when I fire shots it mostly works but every so often a few of the shots will start to wobble around, getting stuck near the edges or spinning around in tight little circles until they timeout and are destroyed. I'm guessing it's some kind of floating point glitch or feedback loop from the raycast, and maybe there's a setting someplace I need to clamp down, but I don't know where to look.
Here's the relevant parts of the shot movement script:
public class MoveScript : MonoBehaviour {
// object speed
public float speed = 10.0f;
private RaycastHit2D hit;
void Update() {
// raycast to check for wall reflection
hit = Physics2D.Raycast(transform.position, transform.up, 0.5f, 1 << LayerMask.NameToLayer("walls"));
}
void FixedUpdate() {
rigidbody2D.velocity = (Vector2)transform.TransformDirection(Vector3.up) * speed;
// wall reflection
if (hit) {
Vector3 dir = Vector3.Reflect (transform.position.normalized, hit.normal);
float angle = Mathf.Atan2(-dir.x, dir.y) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
I noticed that the glitchy behavior became less common when I increased the raycast distance from .1 to .5, but it still persists even with higher values. Any ideas where to look to fix this?
Answer by h_double · Feb 26, 2015 at 03:57 AM
I figured it out.
I forgot to enable isTrigger on the colliders for the shot prefab and playerShip prefab. This caused some confusion in the physics engine, presumably since I am affecting the .velocity directly rather than using AddForce. I don't claim to understand it in detail, but by changing the colliders to isTrigger, it works great.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make a sprite look at mouse on a XY plane ? 1 Answer
Shoot Laser 0 Answers
Distribute terrain in zones 3 Answers