OnTriggerEnter2D being called 25 times.
Hello, I was hoping someone might be able to help me. I am working on a top down game. I have placed water tiles down that I would like to swim through if you interact with the water. I'm guessing that the issue I'm having is because each water tile has the "water" script attached to it, but I'm at a loss as to how to fix it.
Each time I press enter when I'm near water, the storeLocation function I created runs 25 times. It ends up causing my location to be completely incorrect.
Would someone please look through my code and explain what I am doing wrong?
These first code parts are all part of my player_Controller
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
innerWaterColliders = GameObject.FindGameObjectsWithTag ("InnerWaterCollider");
outerWaterColliders = GameObject.FindGameObjectsWithTag ("OuterWaterCollider");
foreach (GameObject colliders in outerWaterColliders) {
colliders.SetActive (false);
}
}
void Update ()
{
if (Input.GetKey (KeyCode.LeftShift)) {
currentSpeed = speed * 2;
isSneak = false;
} else if (Input.GetKey (KeyCode.Space)) {
currentSpeed = speed / 2;
isSneak = true;
} else {
currentSpeed = speed;
isSneak = false;
}
if (Input.GetKeyDown (KeyCode.Return)) {
isInteracting = true;
waterTest ();
} else {
isInteracting = false;
}
if (canMove) {
Vector2 moveInput = new Vector2 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"));
moveVelocity = moveInput.normalized * currentSpeed;
}
void FixedUpdate ()
{
rb.MovePosition (rb.position + moveVelocity * Time.fixedDeltaTime);
}
private void waterTest ()
{
if (isNearWaterEntrance) {
enterWater ();
} else if (isNearWaterExit) {
exitWater ();
}
}
private void enterWater ()
{
foreach (GameObject colliders in innerWaterColliders) {
colliders.SetActive (false);
}
transform.position = tempPos;
foreach (GameObject colliders in outerWaterColliders) {
colliders.SetActive (true);
}
isNearWaterEntrance = false;
}
private void exitWater ()
{
foreach (GameObject colliders in outerWaterColliders) {
colliders.SetActive (false);
}
transform.position = tempPos;
foreach (GameObject colliders in innerWaterColliders) {
colliders.SetActive (true);
}
isNearWaterExit = false;
}
public void storeLocation (GameObject tile)
{
//Debug.Log ("This runs 25 times");
tempPos = tile.transform.position;
tempPos.z = -2;
}
This is the "water" script attached to each tile. The tiles have colliders and triggers attached to them,
public class water : MonoBehaviour
{
public GameObject player;
public player_Controller pc;
public bool isInnerWater;
public bool isOuterWater;
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player");
pc = (player_Controller)player.GetComponent (typeof(player_Controller));
}
private void OnTriggerEnter2D (Collider2D player)
{
if (isInnerWater) {
pc.isNearWaterEntrance = true;
pc.storeLocation (gameObject);
} else if (isOuterWater) {
pc.isNearWaterExit = true;
pc.storeLocation (gameObject);
}
}
private void OnTriggerExit2D (Collider2D player)
{
pc.isNearWaterEntrance = false;
pc.isNearWaterExit = false;
}
}
Your answer
Follow this Question
Related Questions
How do I trigger a game object to move a desired distance at a desired speed? 1 Answer
Unity 5: 2D Platformer: Trigger/Collisions problem 0 Answers
[FIXED] Help with Using Trigger to Move Scenes 1 Answer
Collider/Trigger script without Rigidbody2D component attached not working 0 Answers
enable/disable trigger component with finding objects in scene with tag 1 Answer