void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Powerup")
{
powerUpEnabled = true;
PowerUpStart();
other.gameObject.SetActive(false);
Invoke(nameof(PowerUpEnd),timeLeft);
}
if(other.gameObject.CompareTag("Portal"))
{
isTeleport = true;
initialPosition = transform.position;
originalVelocity = rb.velocity;
rb.velocity = Vector2.zero;
rb.gravityScale = 0;
objMesh.enabled = false;
Invoke("TeleportFinal",FindObjectOfType<CamFollow>().teleportTime);
}
}
void PowerUpStart()
{
rb.mass*=3;
coll.sharedMaterial.bounciness = 0.5f;
Debug.Log("Power up started");
}
void PowerUpEnd()
{
Debug.Log("Power Up ended");
rb.mass/=3;
coll.sharedMaterial.bounciness = 0.97f;
powerUpEnabled = false;
timeLeft = 3f;
}
Also the same code works in 3D (with necessary changes like changing from Collider2D to Collider). I have read some posts about this issue but none of the solutions seem to work for me. Any help would be appreciated as this mechanic is really important to me for a game Jam. We have to submit our game in 1.5 days.
I have tried many things like applying a new physics material to my collider by writing
coll.sharedMaterial = lessBouncy;
Here I defined lessBouncy in the Start() function of my code:
PhysicsMateral2D lessBouncy;
lessBouncy = GetComponent<PhysicsMaterial2D>();
ANY help would be appreciated!
Thanks!