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 kinggamer1235 · Aug 20, 2014 at 05:36 PM · 2dplayerplatformer

player can stick to wall

If the player jumps into a wall and keeps holding the d or a key(depending what side of the game the wall is on) they will just stick to the wall.

Is there any code that could prevent this?

my code for the player:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour 
 {
     public float speed;
     public float jumpSpeed;
 
     bool grounded = false;
     public Transform groundcheck;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
 
     void FixedUpdate()
     {
         grounded = Physics2D.OverlapCircle (groundcheck.position, groundRadius, whatIsGround);
 
         if(Input.GetKey(KeyCode.D))
         {
             rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
         }
         else if(Input.GetKey(KeyCode.A))
         {
             rigidbody2D.velocity = new Vector2 (speed*-1, rigidbody2D.velocity.y);
         }
         else
         {
             rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
         }
     }
 
     void Update()
     {
         if(grounded && Input.GetKeyDown(KeyCode.W))
         {
             rigidbody2D.AddForce (new Vector2 (0, jumpSpeed));
         }
     }
 }

Thanks in advance.

Comment
Add comment
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
1

Answer by AKWolf · Aug 29, 2014 at 01:55 AM

I had the same problem, but in a 3D platformer.

Try reading up on Physics Materials. What was happening in my case was that the velocity vector I was applying to my character kept pushing him into the wall, and the friction between the wall collider and my character collider made him "stick".

What I ended up doing is creating custom physics materials for my wall colliders and my character collider, which solved the problem (essentially I turned friction to be super low for those surfaces). Once you create a new physics material, you can add it on the actual collider component on your GameObject.

Hope that helps!

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 muhammedowais2013 · Oct 03, 2017 at 11:52 AM 0
Share

Its work for me thanks

avatar image
0

Answer by stulleman · Aug 20, 2014 at 05:55 PM

I think you could prevent this with using rigidBody2D.AddForce().

Alternatively, because I know why you dont't want to use AddForce :P, you can just check, like you check if you are grounded, if you touch a wall. If so don't apply any velocity.

Comment
Add comment · Show 4 · 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 kinggamer1235 · Aug 20, 2014 at 06:27 PM 0
Share

thanks! i should have thought of that

avatar image kinggamer1235 · Aug 20, 2014 at 06:51 PM 0
Share

I tried but failed for some reason it wouldn't work, is there something else i can try

avatar image stulleman · Aug 20, 2014 at 08:02 PM 0
Share

Well, what exactly did you do? And what is happening?

avatar image _DkC · Jul 16, 2015 at 11:50 PM 0
Share

@stulleman, I'm not sure if anyone gets notified when I comment, but I hope so.

I check for "IsGrounded" via a linecast centered directly below my character. I find that there is a problem that your movement gets disabled if you move off an edge where the linecast won't touch anything anymore. Having 2-3 different linecast instances with the exact same purpose seems inefficient to me. Is there any other way?

I use Rigidbody2D.addForce() to move my player horizontally and I am currently using Unity 5.1.

Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do I double jump? This isn't supposed to happen... 2 Answers

Enemy collider used as range to detect player causes player to not go into jump animation, how should I deal with that? 0 Answers

Change gravity on button 1 Answer

move, destroy self, instantiate again, repeat. 2 Answers

2D - Detect Slopes x Walls 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