Hi Please help me ,Im making a 2d shooting game,the problem is this
function goFire () {
var bullet = Instantiate(bulletGun , mainPoint.transform.position, Quaternion.identity);
bullet.GetComponent.().velocity = Vector3(22,yBullet,0);
}
my problem is i want to destroy the bullet which is I Instantiate in this function when the bullet collide or hit to the Object I shooting at.
please help…
I'm assuming you have some function to detect if the bullet hit something? void OnCollisionEnter2D(Collision2D coll) { } Depending on where this piece of code is, you can actually just find the bullet by tag or if it is a prefab with a script controlling it, you can just destroy it with Destroy(this.gameObject)
Nevermind, I am wrong. One second let me try to wrap my head around this. I completely understand what you are trying to do, the above script simply slows the character down over X time.
The problem with your script is that, this function is only called when the shoot button is pressed, am I correct?
If not, you could use raycasts to detect collisions on a fixed update.
If it is a rigidbody, you need to set the rigidbody collision detection to continous dynamic.
Next, you put a script on the bullet with the method OnCollisionEnter2D.
Then follow it by the Destroy method. (And whatever else there is to do).
So, you could attach a script on the bullet (then you wouldn’t need to call any methods or classes).
I'm assuming you have some function to detect if the bullet hit something? void OnCollisionEnter2D(Collision2D coll) { } Depending on where this piece of code is, you can actually just find the bullet by tag or if it is a prefab with a script controlling it, you can just destroy it with Destroy(this.gameObject)
– CBRZYNevermind, I am wrong. One second let me try to wrap my head around this. I completely understand what you are trying to do, the above script simply slows the character down over X time.
– b1gry4n