Question by
Kelvynlee · Apr 09, 2019 at 12:48 AM ·
oncollisionenterlocalscale
Change the scale with OnCollisionEnter
When my player collided with a button"GAMEOBJECT" change the scale to big and the button is actived When i collide again change my player scale to small and the button is disabled. How can i do that?
//My player
public GameObject Player;
//Materials
public Material[] material;
Renderer rend;
void Start()
{
//RenderMaterial
rend = GetComponent<Renderer>();
rend.enabled = true;
}
private void OnCollisionEnter(Collision coll)
{
//Change to Big
if(coll.gameObject == Player)
{
rend.sharedMaterial = material[0];
Player.transform.localScale = new Vector3(5, 5, 5);
}
//Change to Small
if (coll.gameObject == Player)
{
rend.sharedMaterial = material[1];
Player.transform.localScale = new Vector3(1, 1, 1);
}
}
}
Comment
Best Answer
Answer by Hellium · Apr 09, 2019 at 06:38 AM
private int collisionsCount = 0;
private void OnCollisionEnter(Collision coll)
{
if(coll.gameObject == Player)
{
collisionsCount++ ;
//Change to big when the object collides with the player the 2nd, 4th, 6th, ... time
if( collisionsCount % 2 == 0 )
{
rend.sharedMaterial = material[1];
Player.transform.localScale = new Vector3(1, 1, 1);
}
//Change to small when the object collides with the player the 1st, 3rd, 5th, ... time
else
{
rend.sharedMaterial = material[0];
Player.transform.localScale = new Vector3(5, 5, 5);
}
}
}
Your answer
Follow this Question
Related Questions
rigidbody.Addforce stacking? 2 Answers
Killing enemy jumping on his head 2 Answers
Check Color of colliding particle? 1 Answer
OnCollisionEnter - Enabled C# 1 Answer