I’m looking to make a Switch/Panel on the ground that can only be weighed down by the player under certain conditions. Does anyone have any recommendations on how to do this?
You can make a script that holds how much weight an object is and then make a function that if an object is colliding with the switch or panel and is the correct weight then activate a bool that says the switch is activated.
Here is an example switch script:
public float weightTilSwitch = 50;
public static bool switchAcitvated;
void Start()
{
switchAcitvated = false;
}
void OnCollisionEnter(Collision other)
{
if (other.collider.GetComponent<Weight>().getObjectWeight() >= weightTilSwitch)
{
switchAcitvated = !switchAcitvated;
if (switchAcitvated == true) {
Debug.Log("Switch Has Been Activated!");
}
else
{
Debug.Log("Switch is inactive!");
}
}
if (other.collider.GetComponent<Weight>().getObjectWeight() < weightTilSwitch)
{
Debug.Log("This Object Does Not Weight Enough!");
}
}
And an example weight script:
public float weight;
public float getObjectWeight()
{
return weight;
}
public void setObjectWeight(float newWeight)
{
weight = newWeight;
}
Thanks!! I ended up adapting your idea by putting a trigger above my switch geometry like so
public float switchActivationweight = 50;
void OnTriggerStay (Collider other)
{
if (other.GetComponent<Rigidbody> ().mass > switchActivationWeight) {
switchActive = true;
} else {
switchActive = false;}
void OnTriggerExit ()
{
switchActive = false;
}
This way I can use the actual Mass of the Rigidbodies in my scene. So if I have a rigidbody change mass for any reason, it will accurately reflect on weighing down the switch.
Now I just need to figure out how to get my switch to animate or at least move up and down when someone stands on it.