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 Tix · May 30, 2013 at 01:33 PM · 2dcollisionbox collider

2d collision with "unwalkable" objects

Hello everyone.

I'm working on a 2D collision script inspired from this tutorial.

I have a player controlled with the key up, down, right and left, and a gameObject with the layer "unwalkable" (layer number 8). What I want is simple: when the player box collider hit the unwalkable box collider, the player is stopped.

I have two problems: Currently when the player hit the unwalkable object, he's stopping... but not always. For example when the player's bottom hit the unwalkable's top, the player run through it:

alt text

Both box collider should not superimposed.

The second problem: for example the player is moving to the top and hit and object, so the player is stopped. But when the player is hitting the object while moving top-right or top-left, he run through it.

Here's an extract of my code:

 private RaycastHit hitInfo;
 private float halfMyX = 18f;
 private float halfMyY = 24f;
 
 private int unwalkable = 1 << 8;
 
 private float absVel2X;
 private float absVel2Y;


 public virtual void characterMove()
     {
         speed.x = 0;
         speed.y = 0;
         
         if (up)
         {
             speed.y = charSpeed;    
         }
         if (down)
         {
             speed.y = -charSpeed;    
         }
         if (right)
         {
             speed.x = charSpeed;
         }
         if (left)
         {
             speed.x = -charSpeed;    
         }
         
         checkBlocked();
         
         speed2 = speed * Time.deltaTime;
         transf.position += new Vector3(speed2.x,speed2.y,0f);
     }

 public void checkBlocked()
 {
     absVel2X = Mathf.Abs(speed2.x);
     absVel2Y = Mathf.Abs(speed2.y);
     
     // blocked right?
     if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), Vector3.right, out hitInfo, halfMyX+absVel2X, unwalkable))
     {
         if (currentFacing == direction.right || currentDirection == direction.right)
         {
             speed.x = 0;
         }
     }
     //blocked left?
     if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), -Vector3.right, out hitInfo, halfMyX+absVel2X, unwalkable))
     {
         if (currentFacing == direction.left || currentDirection == direction.left)
         {
             speed.x = 0;
         }
     }
     //blocked up
     if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), Vector3.up, out hitInfo, halfMyY+absVel2Y, unwalkable))
     {
         if (currentFacing == direction.up || currentDirection == direction.up)
         {
             speed.y = 0;
         }
     }
     //blocked down
     if (Physics.Raycast(new Vector3(transf.position.x,transf.position.y,transf.position.z), -Vector3.up, out hitInfo, halfMyY+absVel2Y, unwalkable))
     {
         if (currentFacing == direction.down || currentDirection == direction.down)
         {
             speed.y = 0;
         }
     }
 }

Here's how the script works: - If the button "up" is pushed, the variable "up" is true. Then the speed is increased. - The checkBlocked() method is called. It set the speed to 0 if the player collide with an unwalkable object.

I could not found a solution for this two problems, maybe I don't know where to search (I tried to find an example of script doing what I want, witch is, I guess, a basic thing...).

Does someone have a solution?

Thank you for your time!

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 jayhatmaker · May 30, 2013 at 03:35 PM

I'm working on a 2D game myself.
It might be just super easier, if you used character controller.
It will simply take care of your character movement.

The problem in this tute tho, add

Debug.Log("your message here");

to any suspicious place and see which function is not working for you.
Hope this helps.

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 Tix · May 31, 2013 at 10:00 AM 0
Share

I heard of character controller, but I didn't thought it will be that easy to solve my problem. Thank you!

But still have an issue, my player object is stopped by every box collider, so I can't make a "walkable" collider (in order to trigger events for example) I'm now moving the player with the $$anonymous$$ove() method. Is there a solution?

avatar image
0

Answer by Tix · May 31, 2013 at 12:31 PM

Nevermind, I checked Is Trigger for the box collider of the object, and my player can walk though!

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.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Pushing against Edge Collider wall stops player from falling 0 Answers

Collision between Character Controller and Box Collider 2 Answers

Physics2D, Box Collider physics inaccuracy? 1 Answer

Colliders 2d apparently touch each other even if they should not. 2 Answers

Collision problem 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