Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by NebulaNu · Jul 28, 2019 at 02:41 AM · scripting problemtriggertiles

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;
     }
 
 }

alt text

water.png (9.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

274 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges