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
0
Question by drodrii · May 22, 2014 at 06:50 PM · gameobjectrigidbodytransformrigidbody2d

Simple - Blocking movement

Hello everyone. I've created 2 gameObjects.

One of them is the player and the other one is an obstacle, I've 4 buttons for movement, I want the object to move around but if he collides with the obstacle I want to prevent movement(While the movement is towards that obstacle, if movement is not towards the obstacle then allow the movement..)

Is there any quick, simple and clean solution to this?

What I've got so far is this script attached to the player.

 public void OnTriggerEnter2D (Collider2D other) {
 
         Debug.LogError ("TriggerEnter");
         Debug.Log (other.gameObject.name);
         canWalk = false;
 
             
     }
 
     public void OnTriggerExit2D (Collider2D other) {
 
         Debug.LogError ("TriggerExit");
         Debug.Log (other.gameObject.name);
         canWalk = true;
         }

But once it collides the player can't move so I can't really exit the collision....

Thank you for your time

Comment
Add comment · Show 4
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 rutter · May 22, 2014 at 07:07 PM 1
Share

Is there a reason you're not just using a conventional collider? Blocking movement is pretty much their job description.

avatar image drodrii · May 22, 2014 at 07:13 PM 0
Share

@rutter

What would be considered a conventional collider?

(The game is on 2D)

avatar image rutter · May 22, 2014 at 07:22 PM 0
Share

By that, I mean a collider which is not set up as a trigger. By default, colliders block movement and triggers do not.

avatar image drodrii · May 22, 2014 at 07:36 PM 0
Share

Oh, ok, so how would I set it up. I should only click isTrigger to 'false' and use onCollision, right?

1 Reply

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

Answer by Kaz_Yamof · May 22, 2014 at 07:53 PM

Well... when you sets canWalk = false in OnTriggerEnter is logical that you can't walk anymore, according your movement logic.

You should try implement something like canWalkFront, canWalkBack, if you don't want to use boxcolliders to control this.

 public void OnTriggerEnter2D (Collider2D other) 
 {
        canWalkFront = false;
        canWalkBack = true;
 }
  
 public void OnTriggerExit2D (Collider2D other) 
 {
        canWalkFront = true;
 }
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 drodrii · May 22, 2014 at 09:01 PM 1
Share

@daybson.paisante

Thanks for your time but what I simply did was turning is$$anonymous$$inematic to true and turn isTrigger to false :D

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

22 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

Related Questions

Need insight on strange (simple) 2D object transform bug 0 Answers

If you change the rigidBody2D.position, you also need to change the gameObject.transform.position? 0 Answers

Take final position after a force applied to a GameObject 1 Answer

Detect a GameObject Passing Between Other Two GameObjects? 4 Answers

Player movement in the direction of rotation INACCURATE? 3 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