• 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 bruu · Feb 28, 2014 at 11:55 AM · movementphysicsrigidbody.velocitysticking

Character getting stuck on walls

Hey,

I have a weird issue where if I jump and hold the movement direction key of the direction I'm jumping in - the character just sticks to the wall. Only when I let go of the movement control, my character stops sticking to the wall. So is there a solution to this? From what I've read, I tried messing around with physical material - and had no luck. It was also suggested that i use Addforce, but the movement speed is too slow- so not desirable.

 using System.Configuration;
 using System.Runtime.InteropServices;
 using UnityEngine;
 using System.Collections;
 
 public class TouchControlManagerWithJump : MonoBehaviour
 {
 
     public GameObject leftController;
     public GameObject rightController;
     public GameObject jumpController;
     public GameObject playerFinder;
 
 
     // Use this for initialization
      void Start()
     {
         //We assign Left and Right GuiTextures to leftController and rightController
         leftController = GameObject.Find("Left");
         rightController = GameObject.Find("Right");
         jumpController = GameObject.Find("Jump");
     }
 
      void FixedUpdate()
     {
         //We assign our main player to playerFinder
         playerFinder = GameObject.Find("pig_test");
 
 
         //Here we create a variable that checks if the player is grounded
         var groundCollisionDet = playerFinder.GetComponent<PlayerGroundDetector>().playerIsGrounded;
 
         //Creating touch controls
         if (Input.touches.Length > 0)
         {
             for (int i = 0; i < Input.touchCount; i++)
             {
                 //setting right touch movement
                 var touch = Input.GetTouch(i);
                 if (this.rightController.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                     {
                         if (groundCollisionDet == true)
                         {
                             /*  
                             old controls with acceleration
                             playerFinder.rigidbody.AddForce(Vector3.right*10, ForceMode.Acceleration);
                             playerFinder.rigidbody.velocity = Vector3.ClampMagnitude(playerFinder.rigidbody.velocity, 10f);
                             */
 
                             //Create a vector 3 and attach playerfinder's rigidbody.velocity to it. Giving it a value.
                             Vector3 playerRight = playerFinder.rigidbody.velocity;
                             playerRight.x = 3;
 
                             //Assigning playerRight to playerfinder
                             playerFinder.rigidbody.velocity = playerRight;
 
                             //Player faces the direction it's moving
                             playerFinder.rigidbody.rotation = Quaternion.LookRotation(Vector3.left);
 
 
                         }
 
                         else if (groundCollisionDet == false)
                         {  
                             Vector3 playerRightAir = playerFinder.rigidbody.velocity;
                             playerRightAir.x = 3;
                             playerFinder.rigidbody.velocity = playerRightAir;
                             
                              
                             playerFinder.rigidbody.rotation = Quaternion.LookRotation(Vector3.left);
                             //playerFinder.rigidbody.AddForce(Vector3.right * 5, ForceMode.Acceleration);
                            
 
                         }
 
 
                     }
 
 
                     else if (touch.phase == TouchPhase.Ended)
                     {
                         playerFinder.rigidbody.rotation = Quaternion.LookRotation(Vector3.forward);
 
                     }
 
                 }
 
                 else if (this.leftController.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                     {
 
                         if (groundCollisionDet == true)
                         {
                             //We do the same here but this time we use '-3' so the player goes the opposite way
                             Vector3 playerLeft = playerFinder.rigidbody.velocity;
                             playerLeft.x = -3;
                             playerFinder.rigidbody.velocity = playerLeft;
 
                             playerFinder.rigidbody.rotation = Quaternion.LookRotation(Vector3.right);
 
                         }
 
                         else if (groundCollisionDet == false)
                         {  
                             Vector3 playerLeftAir = playerFinder.rigidbody.velocity;
                             playerLeftAir.x = -3;
                             playerFinder.rigidbody.velocity = playerLeftAir;
                             
                             //playerFinder.rigidbody.AddForce(Vector3.left*5, ForceMode.Acceleration);
                             playerFinder.rigidbody.rotation = Quaternion.LookRotation(Vector3.right);
                         }
 
                     }
 
                     else if (touch.phase == TouchPhase.Ended)
                     {
 
                         playerFinder.rigidbody.rotation = Quaternion.LookRotation(Vector3.forward);
 
                     }
                 }
 
                 
 
 
                 else if (this.jumpController.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
 
                     if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                     {
                         if (groundCollisionDet == true)
                         {
                             Vector3 playerJump = playerFinder.rigidbody.velocity;
                             playerJump.y = 4;
                             playerFinder.rigidbody.velocity = playerJump;
 
 
                         }
 
                     }
 
                 }
 
 
             }
 
 
         }
 
 
 
 
 
     }
 }
 
 
 
 
 
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
Best Answer

Answer by TurboHermit · Feb 28, 2014 at 12:06 PM

This is a common problem and could be solved multiple ways. What happens is that the force you add in left/right movement is greater than your gravity, resulting in your character being squashed to the wall instead of to the floor.

What I do in many of my platformers is I check if my character is 'walled' as you would check if he's grounded to jump. So if he's actually touching a wall on either side of his body I disable the movement key of that side. I do this via Collision checks, because most of my blocks already have different collision for each side. But you could also do this via Raycasting.

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 bruu · Feb 28, 2014 at 12:23 PM 0
Share

So pretty much (in pseudocode)

If (if raycastHittingwall && playerNotGrounded)

{ //disable this movement

}

So i would have two raycasts shooting out? One from back and the other from the front of the player?

avatar image TurboHermit · Feb 28, 2014 at 01:34 PM 0
Share

Depends if your player 'rotates' by which I mean, if the player is always facing the way you want to move in you only need one ray that checks directly in front of him. If he's facing a wall close-up you disable the ability to move 'forward'.

However if your player can walk both ways without facing any direction in particular, you should indeed raycast two seperate rays and disable that directions movement respectively.

avatar image bruu · Feb 28, 2014 at 05:04 PM 0
Share

$$anonymous$$y character does rotate, but it doesn't seem like the raycast rotates with the player.

avatar image seansteezy · Jul 14, 2014 at 01:10 PM 0
Share

Thanks @Danzou, a simple solution to a simple problem I was having (in the Unity 2D Demo game no less). Beautiful.

avatar image
0

Answer by Simeon · Mar 17, 2016 at 02:58 PM

This might be a bit late, but I've had the same Issues and I tried adding the force needed to push the colliding objects apart. This can be done just by adding the Impulse force in Collision.impulse in the OnCollisionStay method. Like so:

 void OnCollisionStay(Collision collision)
     {
         bool isGround = false;
         foreach (ContactPoint contactPoint in collision.contacts)
         {
             if (Vector3.Angle(Vector3.up, contactPoint.normal) < maxSlopeAngle)
             {
                 isGround = true;
                 grounded = true;
                 maxVelocityChangeSmooth = maxVelocityChange;
             }
         }
 
         if (!isGround)
         {
             Debug.Log(collision.impulse);
             rigidBody.AddForce(collision.impulse, ForceMode.Impulse);
         }
     }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Moving rigidbody (Player) with addForce or Velocity ? 1 Answer

Player is Speeding up in collisions 0 Answers

Rigidbody on a moving tile 1 Answer

How make player move in the air until they hit the ground? 2 Answers

Boat collides with track(plane) below it while moving forward 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