• 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 /
avatar image
0
Question by fLUFFYbUFF · Sep 02, 2017 at 02:55 PM · script.

Why does my code work half the time?

Here is some of the script for the enemy:

 if (isWall) {
     StopAllCoroutines ();
     if (dir == "R" && dir != "L") {
         dir = "L";
         transform.Translate (-1, 0, 0);
         print ("SWITCHL");
         isWall = false;
     } else if (dir == "L" && dir != "R") {
         dir = "R";
         transform.Translate (1, 0, 0);
         print ("SWITCHR");
         isWall = false;
     }
 }

 
 void OnTriggerEnter (Collider other) {
     if (other.gameObject.tag == "SideCollide") {
         isWall = true;
     }
 }

Half of the time the enemy does switch directions when $$anonymous$$tting the trigger. Some times it just goes through and does not stop.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Xepherys · Sep 02, 2017 at 03:22 PM

Well, the root may have somet$$anonymous$$ng to do with collision detection. It may be valuable to set a log inside the OnTriggerEnter. If the speed is too $$anonymous$$gh and/or the object too small, sometimes a collision will not register in a single frame, and somet$$anonymous$$ng can "pass through" the collider. T$$anonymous$$s is part of how physics are calculated. You can try making your walls colliders much larger temporarily as a test to see if that resolves it.

Also, don't check if (dir == "R" && dir != "L"), just check the positive if (dir == "R") as the second part is inherent, but will make a second calculation anyway. If dir == 'R' it inherently cannot also equal 'L'. It's a very tiny $$anonymous$$t, but even t$$anonymous$$s extra calculation could, in theory, take more time than you want during the collision.

Do SWITCHL and SWITCHR print when it passes through the collider, or does it appear not to get to the call at all?

Comment
Add comment · Show 3 · Share
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
avatar image fLUFFYbUFF · Sep 02, 2017 at 03:51 PM 0
Share

Here's an example. So the enemy is moving right. It hits the wall and then it prints "SwitchL". Then it goes left. Then it goes right again and hits the wall. This time it prints "SwitchR" and the enemy goes through the wall.

avatar image Xepherys fLUFFYbUFF · Sep 02, 2017 at 04:36 PM 0
Share

Ohhhh... this might be tough to say without seeing your project, but is it hitting the trigger again? In other words, is it going right again while still intersecting with the collider? I'm guessing this is a geometry or collider issue of some sort.

avatar image fLUFFYbUFF Xepherys · Sep 03, 2017 at 02:43 AM 0
Share

I think so... I'll try to make the trigger a bit smaller.

avatar image
0

Answer by spidermancy612 · Sep 02, 2017 at 03:24 PM

Your code looks fine overall, and if it works sometimes then I suspect there's no fault in what you've provided. My guess would be that the object is either moving too fast or you've got the wrong collision type on.

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

70 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

Related Questions

Please can anyone review my Player Script 1 Answer

How to make a particle system play on trigger with another box collider 1 Answer

rotate an object after moving it 1 Answer

Is there some way to remove and destroy gameobject from list once? 1 Answer

I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers


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