0
votes

I'm working on a game and using the new Tilemap features. They are working fantastic so far. So easy to whip up a level. However I am running into issues when reflecting a laser on the tilemap colliders. As you can see in the GIF below the first few lasers reflect fine, but the sometimes in the small crack where the colliders meet I am getting unwanted behaviour for my laser.

The laser is made up of a small pellet sprite renderer object with a trail renderer for the color behind and a circle collider.

Here is my collision code. Where direction is a class Vector2 variable and rb is theGameObject' s RigidBody2D component stashed in the Start() function.

if (collision.collider.gameObject.layer == LayerMask.NameToLayer ("Terrain")) {
    direction = Vector2.Reflect (direction, collision.contacts[0].normal);
    rb.velocity = direction.normalized*GameManager.instance.bouncySpeed;
}

What steps can I take to avoid this behaviour?

enter image description here

1
Have you tried putting an ELSE on your IF and putting a log there to see if the condition is returning false (some other layer than Terrain)? - C. Parcell

1 Answers

0
votes

I think that the condition returns true since the laser is redirected. But some variable is wrong somewhere.

Try to print direction, collision.contacts[0].normal, collision.contacts.Length, GameManager.instance.bouncySpeed and rb.velocity.

if (collision.collider.gameObject.layer == LayerMask.NameToLayer ("Terrain")) {
    direction = Vector2.Reflect (direction, collision.contacts[0].normal);
    rb.velocity = direction.normalized*GameManager.instance.bouncySpeed;

    Debug.Log("////////");
    Debug.Log("direction: " + direction);
    Debug.Log("collision.contacts[0].normal: " + collision.contacts[0].normal);
    Debug.Log("collision.contacts.Length: " + collision.contacts.Length);
    Debug.Log("GameManager.instance.bouncySpeed: " + GameManager.instance.bouncySpeed);
    Debug.Log("rb.velocity: " + rb.velocity);
    Debug.Log("////////");
}

Compare these values when working and when bugged. Post them here as well if you don't find what's wrong