• 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
4
Question by Leroterr · Oct 25, 2014 at 05:57 AM · collisionmovementphysicsrigidbodycollider

Stopping my player from moving when hitting a wall.

Simple question, but really couldn't find the answer to.

I just want my player to stop moving once it hits wall. What I tried was adding both a Box Collider and a Rigidbody on both objects. Instead of the wall stopping the player from moving, the wall just gets pushed by the player. I tried increasing the mass of the wall but now my player is getting bounced back by the wall, or sometimes the player goes right through the wall. I've tried adjusting the numbers of both objects in several different ways and both of them just keeps going crazy and flipping out around the map.

Is there a simple way of making this without any of the unintended complicated physics stuff happening?

Just a simple 'stop', no sliding bouncing friction complex physics or anything.

Oh and this is for a Top-Down 3D game.

Thanks! :)

Comment
Add comment · Show 2
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 bubzy · Oct 25, 2014 at 07:25 AM 0
Share

is your wall a 3d cube or is it a plane? also make sure is$$anonymous$$inematic is checked on the wall

avatar image $$anonymous$$ · Sep 01, 2019 at 02:38 PM 0
Share

I heard from some of your replies to the answers you got, that your Player Character is jittery when colliding with the wall. $$anonymous$$aybe, you should should change the ,,Collision Detection" in your Player's Rigidbody from ,,Discrete" to ,,Continuous".


If you want an explanation of this: There are 2 Collision Detection modes, with ,,Discrete" being the default, and it's accurate enough for most situations. But sometimes, you need more accurate results at the expense of a bit more processing power, and this is what the ,,Continuous" Collision Detection $$anonymous$$ode is for.

5 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by yashpal · Oct 25, 2014 at 06:09 AM

Hello Leroterr

You don't need to increase mass. Just add rigidbody to your player and collider. and add collider to wall. and put script on player which detects collision and perform action using that.

//This is for 3D game in C#

 void OnCollisionEnter(Collision collision) 
 {
         if(collision.gameObject.name == "YourWallName")  // or if(gameObject.CompareTag("YourWallTag"))
         {
                     rigidbody.velocity = Vector3.zero;
         }
 }

OnCollisionEnter doc

Comment
Add comment · Show 2 · 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 Leroterr · Oct 25, 2014 at 06:37 AM 0
Share

$$anonymous$$y player starts to get "fidgety/jittery" or "shaky" once it hits the wall and me still pressing the forward button. It's like it's still forcing itself to go through the wall (I am, but I don't want the character to act jittery while doing it), then bouncing back a little, then going in again.

I'm also using transform.translate to move my player.

avatar image yashpal · Oct 25, 2014 at 09:19 AM 0
Share

@bubzy just show current way to solve this problem. i think You don't want your walls are moving (means it's static). and jittery effect. you just want to is$$anonymous$$inematic in rigidboady. or don't need to add rigidbody it walls.

avatar image
4

Answer by r3flx · Dec 13, 2017 at 11:12 AM

A surprising good method is actually moving your move script to a FixedUpdate and using rigidbody.position.

try this

 void FixedUpdate()
 {
     float x = Input.GetAxisRaw("Horizontal");
     float z = Input.GetAxisRaw("Vertical");

     playerRb.position += z * transform.forward * Time.deltaTime * speed;
     playerRb.position += x * transform.right * Time.deltaTime * speed;
 }
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 PlatPlayz · Jan 14, 2018 at 08:47 AM 0
Share

$$anonymous$$oving it to FixedUpdate really helped! Thanks a lot!

avatar image
2

Answer by JoshDangIt · Jun 23, 2016 at 08:23 PM

Make the wall's rigidbody Kinematic. Then make sure the player's collider doesn't have a bouncy PhysicsMaterial.

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
0

Answer by Maginix · Jun 24, 2016 at 05:01 AM

 it works perfectly :)
 
 void LateUpdate()
     {
         //fix flying if is blocked on jump
         if(charController.velocity.y == 0)
         {
             moveDirectionSmoothly.y = charController.velocity.y;
         }
             
         //fix "walk stopped" (stop the character if it is blocked by a wall)
         if(charController.velocity.z == 0)
         {
             moveDirectionSmoothly.z = charController.velocity.z;
         }
     }

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
avatar image
0

Answer by Pathojen · Mar 23, 2019 at 07:50 PM

I have the same question, only with a 2D object. Any advice? Thanks in advance.

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 Llama_w_2Ls · Jul 20, 2020 at 03:09 PM 0
Share

@Pathojen you could add some drag. This way, when the rigidbody 2D hits the wall, it may bounce back but its velocity is dramatically reduced over time until it comes to a stop.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

39 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

Related Questions

Velocity data from OnCollisionEnter is delayed (incorrect) 3 Answers

How to properly move a rigidbody/collider? 2 Answers

Layers are colliding even though they are set to ignore each other 1 Answer

Camera gets flung off of the map when the player collides with certain objects. 0 Answers

OnCollisionEnter but have colliding object not move the object it collides with 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