Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 PastaMilk · Jan 31 at 10:28 AM · unity 2dbooleanontriggerenter2d

OntriggerEnter2d not changing Boolean C#

Hello!

I'm having an issue where my moveRight bool is not changing when I'm colliding with an object tagged as a "Wall" but only when moveRight is marked as true too. If the Player (White square) collides with a Wall when moveLeft is True then the Player changes direction as expected. Gif and code is below. Any insight would be much appreciated!

Goal is to have the player constantly move in one direction based on the last key press OR if the player collides with a wall and forces a change in direction.

 public class Character2DController : MonoBehaviour
 {
     public float movementSpeed;
     public float jumpForce;
     private Rigidbody2D _rb;
     public bool moveRight, moveLeft;
 
     void Start()
     {
         _rb = GetComponent<Rigidbody2D>(); //declare RB for jumping
         moveRight = false;
         moveLeft = false;
     }
 
     void Update()
     {   
         if(Input.GetKeyDown(KeyCode.D))
         {
             moveRight = true;
             moveLeft = false;
         }
          if (moveRight)
          {
             transform.position += new Vector3(1, 0, 0) * Time.deltaTime * movementSpeed;
          }
 
         if (Input.GetKeyDown(KeyCode.A))
         {
             moveLeft = true;
             moveRight = false;
         }
           if (moveLeft)
          {
             transform.position += new Vector3(1, 0, 0) * Time.deltaTime * -movementSpeed;
          }
 
         //jumping
         //by checking the y impulse velocity this limits jumping to only a single instance 
         if (Input.GetButtonDown("Jump") && Mathf.Abs(_rb.velocity.y) < 0.001f) 
         {
             _rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
         }
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Wall" && moveRight)
         {
             moveRight = false;
             moveLeft = true;   
         }
 
         if (other.tag == "Wall" && moveLeft)
         {
             moveRight = true;
             moveLeft = false;
         }
     }
 }

alt text

![alt text][2]

collider-issues2.gif (435.1 kB)
collider-issues2.gif (435.1 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

185 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

Related Questions

my animtion plays when its not ment to too 0 Answers

Need help with writing a bool to call a function 0 Answers

how to stop at a certain line of code?? 0 Answers

While OnTriggerStay i need to press n hold direction button to get input from a key, why? 1 Answer

Unity2D: How to make an object pick up another object and carry it around? 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