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 onionman99 · Jul 17, 2013 at 07:07 AM · 2dcharacterwall

when 2d character hits wall it shakes/bounces

i am new to this and am making a small 2d game to learn. when my character hits the wall it shakes or bounces if you like. in my first level i made constraints in the script to stop this. however this level is not a square like the first level. is there any way i can make a wall without it making the character shake?

Comment
Add comment · Show 9
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 Sospitas · Jul 17, 2013 at 09:27 AM 0
Share

Do you mean the character is see$$anonymous$$gly entering the wall and then being placed back outside of the wall repeatedly?

avatar image onionman99 · Jul 17, 2013 at 09:29 AM 0
Share

pretty much yeah

avatar image Sospitas · Jul 17, 2013 at 09:31 AM 0
Share

I'm assu$$anonymous$$g you are using Translation for your movement then?

avatar image onionman99 · Jul 17, 2013 at 09:32 AM 0
Share

yes, i didnt realise there is another way

avatar image Sospitas · Jul 17, 2013 at 09:36 AM 1
Share

Yeah, Translation will move your character in set amounts, so if your characters position + your movement value is within the wall, it will move you within the wall instantly, and then resolve the collision pushing you back out.

There are two ways you could go. You could either perform a check before moving to see if you would intersect a wall, and then alter your movement value to make sure that you do not ever end up inside of a wall, or you could switch your movement code to using velocities, however this could require a significant amount of work depending on how sophisticated your movement code already is.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sospitas · Jul 17, 2013 at 09:56 AM

You could create a Ray from the players centre to either side, and then calculate the length of that ray. If the length is < your translation value, then adjust the translation value to match the distance (probably have to account for your collider bounds as well), something like this should suffice:

 function CheckMovement()
 {
     RaycastHit hitL, hitR;
 
     if(Physics.Raycast(player.transform.position, Vector3.left, out hitL))
     {
         if(hitL.distance < translationMovementValue)
         {
             translationMovementValue = hitL.distance;
         }
     }
 
     if(Physics.Raycast(player.transform.position, Vector3.right, out hitR))
     {
         if(hitR.distance < translationMovementValue)
         {
             translationMovementValue = hitR.distance;
         }
     }
 }

To be honest, you could probably split that up so that it will only Raycast to the left or to the right when you press the corresponding movement key, which would be better. But that should be a good starting point for now :)

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

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

2D Character Switch Systsem (Same Location) 1 Answer

Non-symmetrical Character walk left animation. 0 Answers

"wheel" character 2 Answers

Android 2D Character Controller 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 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