Question by
boomtom · Oct 01, 2020 at 08:59 AM ·
collision detectiononcollisionexit
How do I make a pressure plate register when the player is on top of it, and when a player gets off of it?
I made a pressure plate that moves a bridge up when the player touches it, but now I want to make one that reverses if the player gets off of it. I tried what I thought would work, but instead it allowed the pressure plate to raise back up, but the bridge doesn't begin to go back down. Here is my pressure plate code so far:
public class PressureHold : MonoBehaviour
{
public bool isOpened;
public float movementSpeed = 10;
[SerializeField] Vector3 position;
[SerializeField] Vector3 endPosition;
void Start()
{
isOpened = false;
position.Set(transform.position.x, transform.position.y, transform.position.z);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
isOpened = true;
if (position != endPosition)
{
transform.position = Vector3.MoveTowards(transform.position, endPosition, movementSpeed * Time.deltaTime);
}
}
}
void OnCollisionExit(Collision col)
{
if (col.gameObject.tag != "Player")
{
isOpened = false;
transform.position = Vector3.MoveTowards(transform.position, position, movementSpeed * Time.deltaTime);
}
}
}
Here is my bridge code:
public class MoveUpBridge : MonoBehaviour
{
public PressureActivate pA;
public float movementSpeed = 10;
[SerializeField] Vector3 position;
[SerializeField] Vector3 endPosition;
void Start()
{
pA.isOpened = false;
position.Set(transform.position.x, transform.position.y, transform.position.z);
}
void Update()
{
if (pA.isOpened == true)
{
if (transform.position != endPosition)
{
transform.position = Vector3.MoveTowards(transform.position, endPosition, movementSpeed * Time.deltaTime);
}
}
else if (pA.isOpened == false)
{
if (transform.position != position)
{
transform.position = Vector3.MoveTowards(transform.position, position, movementSpeed * Time.deltaTime);
}
}
}
}
Comment