The game is a top down 2d shooter and I'm facing trouble in doing this : - I got a player 2D ship - The player fires laser, which is a long thin image with box collider attached. - What I want is that when the laser hit the enemy ship, the laser's length (Height) should be equal to the distance between the player and the enemy. (That is the laser shouldn't go through the enemy)
So what I have done is this :
In the Laser's script :
Vector3 scale = new Vector3(1, 1, 1);
void OnCollisionStay2D(Collision2D col) { If (col.gameObject.layer == 12) //ENEMY { scale.y = (col.gameObject.transform.position.y - Info.PlayerPosition.y) / this.gameObject.renderer.bounds.size.y; this.transform.localScale = scale; }
void OnCollisionExit2D(Collision2D col) { scale.y = 10 / this.gameObject.renderer.bounds.size.y; // 10 is the height of the screen; this.transform.localScale = scale; }
That's it. So when I run it, things go crazy, the scale.y value kind of oscillates between some low value and high value and withing 2-3 seconds, scale.y becomes infinity (as of Unity Console)
I can't make anything out of it :( Been like 1.5months with unity. Thanks in advance :)