Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Kensei · Mar 31, 2014 at 10:04 AM · c#charactercontrollerif-statementslogic

False = true, what am I doing wrong?

Guys I'm having trouble enabling and disabling a jump. It all worked fine until I added a horizontal movement controller, and now even if I remove it it still doesn't work. I can't find a logic problem and I've re-read my code so many times. Please help me find the issue.

 using UnityEngine;
 using System.Collections;
 
 public class Bouncer : MonoBehaviour {
 
     public float jumpForce;
     public float moveSpeed;
     public int jumpAttempts = 5000;
     public int totalJumps;
     public int successfulJumps;
 
 
 
 
 
 
     private bool jump = false;
 
     void OnTriggerExit2D (Collider2D wall)
     {
         if (wall.tag == "Border")
         {
             jump = false;
             Debug.Log ("Exited");
         }
     }
     
     
     void OnTriggerStay2D (Collider2D wall)
     {
         if (wall.tag == "Border")
         {
             jump = true;
         }
     }
 
 
     void Update ()
     {
         // Change world axis to local axis.
         Vector3 direction = transform.InverseTransformDirection (Vector3.up); 
 
         // Set the jumping controls.
         if (Input.GetButtonDown ("Jump") && jump && jumpAttempts > 0)
         {
             rigidbody2D.AddForce (new Vector2(direction.x,direction.y) * jumpForce * successfulJumps);
 
             // Configure jump rules.
             jumpAttempts = jumpAttempts - 1;
             successfulJumps = successfulJumps + 1;
             totalJumps = totalJumps + 1;
 
             jump = false;
 
             Debug.Log ("Jumps Left:"+jumpAttempts);
         }
 
         // Punish the player for failure.
         if (Input.GetButtonDown ("Jump") && !jump && jumpAttempts > 0)
         {
             jumpAttempts = jumpAttempts - 1;
             successfulJumps = 1;
             Debug.Log ("Killed Jump, counter reset to" + successfulJumps);
         }
 
     }
 
 
 
     void FixedUpdate ()
     {
         float horizontalInput = Input.GetAxis ("Horizontal");
 
         if (Input.GetAxis ("Horizontal") != 0)
         {
             rigidbody2D.AddForce (new Vector2 (horizontalInput,0) * moveSpeed);
         }
     }
 
 }

With this code, if my player enters a trigger for the first time, jump is enabled, but then its disabled even if he stays in the trigger. It seems the false fires off during true, please tell me where is the problem, I'm almost ready to blatantly copy -> paste the character controller from Unity's 2D platformer...

Comment
Add comment · Show 1
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 patrik-org · Mar 31, 2014 at 12:35 PM 0
Share

What does your logs say? Is "Exited" printed even if the player stays in the trigger? Also, a tip is to set jump to a public variable in order to easier see its value changing in the inspector window.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 31, 2014 at 01:53 PM

Your logic error is probably inside Update. The two if statements (line 44 and line 59) are always simultaneous true. Why? well that's simple:

First of all both if statements are executed in a row. Both need jumpAttempts to be greater than 0 and the jump button pressed. The only difference is that the first needs "jump" to be true while the second needs "jump" to be false. The problem arises from the line 53 inside the first if's body where you set "jump" to false.

That means the second if is always true when the first is true. To shorten what happens look at this equivalent of your code:

 if (Input.GetButtonDown ("Jump") && jumpAttempts > 0)
 {
     if (jump)
     {
         //[first if body]
         jump = false;       // this makes the second if also true.
     }
     
     if (!jump)
     {
         // [second if body]
     }
 }

This is what happens in your code. Since the jump is set to true every frane inside TriggerStay jump is always true when you enter Update. So both if-statements are executed at once.

Are you sure you want OnTriggerStay2D? Keep in mind it's executed every frame as long as you're inside the trigger.

It's difficult to understand what this is all about. It looks like some kind of wall-jumping. A short explanation would be nice ;)

I guess you might want to use my shorten version of your if construct but use an else like this:

 if (Input.GetButtonDown ("Jump") && jumpAttempts > 0)
 {
     if (jump)
     {
         //[first if body]
         jump = false;
     }
     else
     {
         // [second if body]
     }
 }
Comment
Add comment · Show 1 · 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 Kensei · Mar 31, 2014 at 05:03 PM 0
Share

Ahhh I can't believe it was staring me in the face. Yes you are right, thanks.

Yeah its a wall jumping thing, but ultimately I ended up scrapping this controller and opted out for a Physics2D.OverlapCircle collider check in order to enable jumps. Nevertheless thank you for the answer, I can't believe I didn't realize it myself :)

avatar image
0

Answer by NDJoshi · Mar 31, 2014 at 01:22 PM

Its very simple bug if i am getting your problem in your code.

In, void OnTriggerStay2D (Collider2D wall) { if (wall.tag == "Border") { jump = true; } } you just need to add else part...

void OnTriggerStay2D (Collider2D wall) { if (wall.tag == "Border") { jump = true; } else { jump = false; } } According to me this should work perfectly.

Comment
Add comment · Show 1 · 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 Bunny83 · Mar 31, 2014 at 02:10 PM 0
Share

That wouldn't help much. OnTriggerStay2D is only called when you actually touch / intersect a trigger. Once he left the "Border" OnTriggerStay2D isn't called anymore unless he's intersecting another trigger. OnTriggerExit2D is the proper way to deter$$anonymous$$e that you don't intersect anymore.

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

24 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

Related Questions

Distribute terrain in zones 3 Answers

C# Character Controller Collision If Statement 2 Answers

Check if all values in a list are equal 1 Answer

Multiple Cars not working 1 Answer

What's the correct input for character movement 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