increase or decrease a value by 1 depending of current condition
hi, im making a game based on pico park, i've been trying to replicate the box mechanic(x players needed to move a box) but i have a problem, i have a float called "playersneeded" that decreases when a player touches a box and increases when it gets out of collision(using OnCollisionEnter/Exit), but i dont know how to update that value based of if im currently pushing or not the box, i've been trying for a week to find a way to do it but i cant find anything, i've tried using OnCollisionStay for it but the value keeps incrementing. currently this is the code i use for the box:
using UnityEngine;
public class Box : MonoBehaviour
{
private GameObject player;
private GameObject number;
public bool Pushed;
void Start()
{
player = GameObject.FindWithTag("Player");
number = GameObject.Find("Box/number");
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Box" && other.transform.position.y > transform.position.y)
{
other.transform.SetParent(transform);
}
if (other.gameObject.tag == "Player" && other.transform.position.y < transform.position.y)
{
Pushed = true;
GetComponentInChildren<NumberChanger>().currentPlayers++;
GetComponentInChildren<NumberChanger>().PlayersNeeded--;
gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
}
}
private void OnCollisionStay2D(Collision2D other)
{
if (other.gameObject.tag == "Box" && other.transform.position.y > transform.position.y)
{
other.gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
}
if (other.gameObject.tag == "Player" && other.transform.position.y < transform.position.y)
{
Pushed = true;
gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
}
if (other.gameObject.tag == "Player" && player.GetComponent<playercontroller>().IsPushing == false && other.transform.position.y < transform.position.y)
{
Pushed = false;
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "Box")
{
other.transform.SetParent(null);
}
if (other.gameObject.tag == "Player" && player.GetComponent<playercontroller>().IsPushing == false && other.transform.position.y < transform.position.y)
{
Pushed = false;
GetComponentInChildren<NumberChanger>().currentPlayers--;
GetComponentInChildren<NumberChanger>().PlayersNeeded++;
}
}
}
to be clear, i want it to increase by 1 if the player is currently touching the box but not moving and decrease by 1 if the player is touching and moving it. hope someone can help me. thanks
Your answer
Follow this Question
Related Questions
Collecting puzzle pieces in my platformer game 0 Answers
Defining Rectangles on the fly 1 Answer
My Click and drag object moves slowly after I drop it 0 Answers
2D puzzle Game 1 Answer