Question by
unity_XzfbCCSKRX8b3w · Jul 07, 2020 at 05:51 AM ·
gameobjectsoncollisionenter
How to create two game object of half the size of original one when it is hit by a bullet?
This is the code i have written in on Collision 2d enter of the original game object. if (collision.gameObject.tag == "Bullet" ) {
if (!flagBulletCollided)
{
flagBulletCollided = true;
CircleCollider2D collider2D;
Vector3 localScale = transform.localScale;
Vector3 position = transform.position;
collider2D = GetComponent<CircleCollider2D>();
float radius = collider2D.radius;
float bulletRadius = collision.gameObject.GetComponent<CircleCollider2D>().radius;
localScale.x = localScale.x / 2;
localScale.y = localScale.y / 2;
position.x += position.x + 2 * bulletRadius;
position.y += position.y + 2 * bulletRadius;
radius = radius / 2;
collider2D.radius = radius;
transform.localScale = localScale;
transform.position = position;
Transform temp = transform;
Instantiate<GameObject>(gameObject, temp);
Instantiate<GameObject>(gameObject, transform);
transform.DetachChildren();
Destroy(gameObject);
There are three game object of .25 scale of the original game object.
Comment
Your answer
Follow this Question
Related Questions
How can i add a gameobject configured already on a unity application without rebuild this? 0 Answers
destroying instantiated groups of objects 1 Answer
Bad practice to create a GameObject for every BoxCollider2D? 0 Answers
Any way to create multiple empty GameObjects from a list? 0 Answers
Why can I not pick up my moving/wandering objects yet I can pick up those that stay in one place? 0 Answers