- Home /
Weighing down a heavy switch
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?
Answer by KittenSnipes · Jan 14, 2018 at 02:31 AM
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;
}
Answer by thestrandedmoose · Jan 15, 2018 at 07:20 PM
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.
Your answer
Follow this Question
Related Questions
In game right click menu 1 Answer
On mouse down switch on / off 2 Answers
How to play two animations with one UI button? 1 Answer
Enabling/Disabling a slider from script 0 Answers
OnPointerExit not updating if container panel was SetActive(false) 2 Answers