- Home /
Resizing an object on collision in unity,Resizing an Object on Collision in unity
I have two objects - a bunch of asteroids and a sphere-CollisionSphere. Asteroids on collision with the sphere have to be destroyed and the sphere has to increase in size. As of now, the asteroids are getting destroyed- no problem however the size doesn't seem to be changing of the sphere.
Could someone please help me...
this is my code
Vector3 temp;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "CollisionSphere")
{
//Destroy(gameObject);
gameObject.SetActive(false);
temp += new Vector3(2, 2, 2);
transform.localScale = temp;
}
}
$$anonymous$$ake temp public so you can check that it's actually changing.
Answer by handerhedeghog · Nov 24, 2019 at 02:35 PM
If this is your asteroid code, then why do you try to use temp varible? This will not work anyway, so let's replace this with: collision.gameObject.transform.localScale +=new Vector3(2, 2, 2); 2.Why do you trying to change scale of asteroid. 3.Destroy asteroid AFTER change scale.
There you go fixed code:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "CollisionSphere")
{
collision.transform.localScale += new Vector3(2, 2, 2);
//Destroy(gameObject);
gameObject.SetActive(false);
}
}
Your answer
Follow this Question
Related Questions
Detecting When Intersection Penalty is Being Applied 2 Answers
Ignore Collision Temporarily 1 Answer
Scaling a collider to set an event 1 Answer
How to get expanding object collide when it touches others? 1 Answer
Colliders won't work 0 Answers