Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheRedMezek · Oct 13, 2017 at 05:19 PM · collisionrigidbodyboxcollider

Rigidbody player slightly phasing into wall

I have a very basic player controller script:

 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed = 1;
     private Rigidbody body;
 
     void Start () {
         body = gameObject.GetComponent<Rigidbody>();
     }
     
     void FixedUpdate()
     {
         body.transform.Translate(Input.GetAxis("Horizontal") * moveSpeed,
                                  0,
                                  Input.GetAxis("Vertical") * moveSpeed);
     }
 
 }

Mostly it works, but when I move the player (currently using a simple cube) into a wall (also simple blocks) and continue holding down the movement key, the player phases into the wall just a bit. When I release the movement key, they go back out to where they ought to be colliding with it. When phasing into walls, it's possible to move the player through spaces way more narrow than the character is, and sometimes to glitch through walls entirely.

I've tried using a CharacterController component instead of a Rigidbody, but then the player won't interact with physics objects and I get the inverse problem I'm having now: the player collides with the walls just a bit outside the wall, unless I shimmy back and forth, in which case it slowly slides forward until the player is actually up against the wall.

I'm stumped. This is an extremely basic and simple situation and I seem to be suffering from some part of the Unity physics engine I don't understand.

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 Glurth · Oct 13, 2017 at 05:32 PM 0
Share

Can you confirm for us, that the collider on your object, and the collider on the walls exactly match their visible shapes? (box collider on cube, sure SOUNDS like it)

avatar image TheRedMezek Glurth · Oct 13, 2017 at 05:48 PM 0
Share

Yes, that was one of the first things checked. They're all box colliders atm, and they exactly match their visible shapes.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vollmondum · Oct 14, 2017 at 06:47 AM

I'd not go with physics, it's lame. Instead make your wall doubled, with outer invisible shell, and let your player stick inside it, leaving visible obstacle unreached.

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 FlyingHighUp · Oct 14, 2017 at 04:29 AM

I'm pretty sure this is because you're moving your player with Translate(), which internally simply sets transform.position.

Setting transform.position essentially teleports your player into the wall. Then, PhysX takes over and tries to push your player out of the wall. This is typically a bad idea, because if the teleport distance is great enough the player can go right through.

Instead of Translate use rigidbody.AddForce():

 rigidbody.AddForce(new Vector3(Input.GetAxis("Horizontal") * moveSpeed,  0.0f, Input.GetAxis("Vertical") * moveSpeed));

Since PhysX has an idea where the player is going, it'll do better collision tests and move the player for you. Setting rigidbody.velocity might seem like a better choice at first, but it can suffers from the same issues as Translate() does. In my experience AddForce() (begrudgingly) is really the most stable method.

It's a bit more tricky to code - especially if you're doing cartoony movement. However, the benefits of letting PhysX do more of the heavy lifting saves a lot of headaches over that pile-of-crap character collider. Past this, if your player still tunnels (typically only a problem if you're travelling at intense speeds), change the collision solving to Continuous Dynamic, and PhysX will use more expensive tracing calculations to ensure objects don't pass through each other.

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 TjazS · Nov 08, 2020 at 02:45 PM

I would rather go with character controller. It would not pass into a wall

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

131 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

Related Questions

Box Colider not working properly 1 Answer

Box collider (with Rigidbody attached) gets stuck into another Box Collider 0 Answers

Rigid body not colliding with box collider. 1 Answer

Collision Detection for Kinematic Rigidbodies 0 Answers

Box collider gets stuck on edges of tiled floor, cube bounces up 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