Hi, I’m trying to make a game where one has to catch bars of soap. I want the player to catch the soap and hold it until it shrinks to a fraction of its original size.
This mechanic works for the most part, but once I reach a certain threshold, it shrinks down to the minimum size on its own, and I don’t know why. Below is a video demonstrating the problem as well as the relevant code.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewSoap : MonoBehaviour {
public float hp = 300;
float totalHp;
public float minHp;
float scaler;
private bool isCaught = false;
Vector3 originalScale;
// Use this for initialization
void Start() {
totalHp = hp;
originalScale = new Vector3(GetComponent<Transform>().localScale.x, GetComponent<Transform>().localScale.y, GetComponent<Transform>().localScale.z);
}
// Update is called once per frame
void Update() {
print("health: " + hp + "|| scaler: " + scaler);
if ((hp >= minHp) && isCaught)
{
hp--;
scaler = hp / totalHp;
GetComponent<Transform>().localScale = originalScale * scaler;
}
else if( hp <= minHp)
{
// Destroy(gameObject);
}
}
void OnTriggerStay2D(Collider2D other)
{
if ((other.name == "Hand") && (other.GetComponent<Hand>().isOpen() == false))
{
isCaught = true;
print("Caught!!");
}
else
{
isCaught = false;
}
}
void onTriggerExit2D(Collider2D other)
{
if (other.name == "Hand")
{
isCaught = false;
}
}
}
Any help would be greatly appreciated, thank you!!