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 /
  • Help Room /
avatar image
0
Question by Skinny_Mini_Man · Mar 15 at 09:39 PM · rigidbody-collision

Circle kind of clipping in the wall

So, I'm making my first game (a platformer) and (at least for now) you are playing as a circle, but whenever I move into a wall (move forward into a wall continuously) the circle is somewhat glitching into a wall a little bit and it also causes for the character to fall a little slower. It glitches like one frame it is in the wall, another it is just normally touching the wall. I don't know what could cause such behavior. Anybody knows how to fix this?alt text

image-2022-03-15-223911.png (679 B)
Comment
Add comment · Show 1
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 Blueshrimps · Mar 16 at 06:32 PM 0
Share

I was making a 2d platformer myself a while ago, and I had similar problems until I did a bit of research and created a script that worked well.

public class mainScript : MonoBehaviour { private Rigidbody2D rb; private float moveInputH; public float speed = 15; public float jumpSpeed, xOffset, yOffset, xSize, ySize; public bool isGrounded; public LayerMask groundMask; float horizontalMove = 0f; public float runSpeed = 4f;

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     jumpSpeed = 15;   
 }

 void Update()
 {
     moveInputH = Input.GetAxisRaw("Horizontal");

     rb.velocity = new Vector3(moveInputH * speed, rb.velocity.y);

     float horizontal = Input.GetAxis("Horizontal");



     isGrounded = Physics2D.OverlapBox(new Vector2(transform.position.x + xOffset, transform.position.y + yOffset), new Vector2(xSize, ySize), 0f, groundMask);

     if(Input.GetKeyDown(KeyCode.UpArrow) && isGrounded)
     {
         rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
     }

     if(Input.GetKeyDown(KeyCode.W) && isGrounded)
     {
         rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
     }

     horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

 }
     
     private void OnDrawGizmosSelected()
     {
     Gizmos.color = Color.red;
     Gizmos.DrawCube(new Vector2(transform.position.x + xOffset, transform.position.y + yOffset), new Vector2(xSize, ySize));
     }

}

There was originally some code for animation and flipping directions, but as a circle, your character probably won't need that. You might think that this wouldn't work because it has no way to detect keys other than arrows, but unity moves it when a key is pressed automatically. This is tried and true, and worked really well for me. Let me know if it works. Good luck!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Blueshrimps · Mar 16 at 02:12 PM

Are you using a rigidbody, or a character controller to move?

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 Skinny_Mini_Man · Mar 16 at 05:18 PM 0
Share

I'm using rigidbody2D.

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

176 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 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

Trying to prevent click& drag camera from going through mesh, rigidbody makes it bounce like crazy 0 Answers

How to prevent a Rigidbody on collision to NOT do anything? 0 Answers

My player's rigidbody 2d is being continuously pushed downwards by box collider 2d 1 Answer

Rigidbody Position And Rotation Freeze makes player go through the object 0 Answers

Pushable Boxes in 3d ISO game 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