So, I am trying to make a game where a spaceship shoots a bullet and if the bullet hits one of the side walls its velocity.x is flipped and it mimics a bounce of the wall. The bullet I have is more like a laser and has an angle that matches its velocity.
What I want to happen when the “laser” bounces off one of the walls is for it to flip its z rotation as well to correctly mimic its new velocity.
Right now I have the initial velocity set to the angle of the space ship who shoots it.
here is the code of when the bullet hits the wall:
void OnTriggerEnter2D(Collider2D coll) {
if (coll.tag == "Wall") {
Destroy (this.gameObject);
} else if (coll.tag == "Bouncy Wall") {
//inverse bullet velocity and transform rotation
bulletRB.velocity = new Vector2(bulletRB.velocity.x * -1, bulletRB.velocity.y);
transform.localEulerAngles = new Vector3(0,0, - transform.rotation.z);
}
}
This i think is the correct way to do it but what happens I think is when the “laser” is flipped part of its collider re-enters the trigger collider causing it to go to rotation.z = 0 (straight up and down).
I hope I am explaining this correctly… Let me know if you need anything else…