- Home /
Question by
Kelvynlee · Apr 09, 2019 at 01:07 AM ·
unity5oncollisionenteriflocalscaleif-else
Change the scale with OnCollisionEnter once
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
Track the state of the button.
bool State = false;
private void OnCollsionEnter(Collision coll){
if(coll.gameObject == Player){
if(State){
//Enlarge Code
}else{
//Shrink Code
}
State = !State;
}
Answer by Nivbot · Apr 09, 2019 at 04:11 AM
Just make a bool.
edit: Decided just to put the complete thing..
private void OnCollisionEnter(Collision coll)
{
//Change to Big
if(coll.gameObject == Player)
{
if(isBig)
{
rend.sharedMaterial = material[0];
Player.transform.localScale = new Vector3(1, 1, 1);
isBig = false;
}
else
{
rend.sharedMaterial = material[1];
Player.transform.localScale = new Vector3(5, 5, 5);
isBig = true;
}
}