• 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
Question by squigglyo · May 13, 2013 at 10:04 AM · 2dmovementrigidbodycharactercontroller

2D rigidbody movement & walls

Im having some issues with my 2d movement in my game.

If the character jumps while moving into a wall, they will get stuck, with the collider being slightly inside the wall.

The gameobject has a rigidbody as well as a box collider. The walls have a collider. I have been told that using Translate is not the proper way to move rigidbodys, but the project is tens of thousands of lines long and changing the way movement works just isnt something that I want to consider at this point. So a way to check the surrounding areas to make sure the player isnt about to move into a wall is what I need.

 movement = 1;
 movement *= gameData.GetDelta();
 thisTransform.Translate(movement, 0, 0);    
 

         
Comment

People who like this

0 Show 0
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

Answer by DESTRUKTORR · May 13, 2013 at 09:04 PM

The reason that translate isn't a proper way to do it is because it doesn't apply the physics calculations during interactions between colliders. That is to say, you're trying to reinvent the wheel, with the physics engine. I'd hate to say it, but your best bet (without changing to a velocity/force-based movement system) would be to check collisions and apply counter-force.

Why would you assign movement to 1 then multiply it by something...? 1*(someNumber) = someNumber... if you've programmed like this throughout the project, you'll probably want to go back and change a lot of things, anyway. Also, is this a monobehaviour? If so, why have you used up another reference to refer to the transform? You should change this all down to the following:

 transform.Translate(gameData.GetDelta(),0,0);

However, the most efficient way to manually check if the player is moving into a wall is to check it on the frame in which it occurs, or to do a raycast/spherecast in the direction of movement. However, this can get very expensive very quickly, and I STRONGLY suggest you change to velocity/force-based movement.

Comment
Superwayne

People who like this

1 Show 0 · 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

Answer by Superwayne · May 13, 2013 at 09:21 PM

The only thing I could imagine would be: Create a new script for the wall with "OnCollisionEnter", check for the player GameObject and translate it to a place, where it's not stuck in the wall. With a simple if/else you could chose on which side the player should be translated by comparing player.transform.position.x and transform.position.x (considering x is your horizontal axis)

This is not an optimal solution but the best I can imagine without changing your movement..

Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Making character dash forward 0 Answers

Projectile Ricochet Issue. Travels along the surface rather than reflecting off of it 2 Answers

Why does my physics2D script cause the player to randomly stop moving? 0 Answers

if CharacterController hit a rigidbody 1 Answer

Top-down movement in 2D 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