• 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 ishaancoding21 · Feb 18, 2020 at 09:18 PM · charactercontrollervelocityplatformer

How prevent my player from getting stuck on the wall/clinging onto it if I am moving towards it (rb.velocity) with an arrow key at the same time?

I am making a 3D game where the character moves around in a world. When I touch a wall (even in midair) and I press an arrow key which is moving the player in the direction towards the wall, he gets stuck. How do I make it so that he just slides off like normal, such as Mario when he just falls/slides off of the wall?

My Code:

public class Player : MonoBehaviour { public Rigidbody rb; private float Movespeed = 10f; public GameObject player; public Animator player_anim; public bool grounded; public float jumpForce;

 // Start is called before the first frame update
 void Start()
 {
     rb = GetComponent<Rigidbody>();
     grounded = false;
 }

 private void FixedUpdate()
 {
     rb.velocity = new Vector3(Input.GetAxis("Horizontal") * Movespeed, rb.velocity.y, Input.GetAxis("Vertical") * Movespeed);

     if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
     {
         player_anim.SetBool("Moving", true);
     }
     if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
     {
         player_anim.SetBool("Moving", false);
     }

     Vector3 rotation = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
     if (rotation != Vector3.zero)
     {
         player.transform.rotation = Quaternion.LookRotation(rotation);
     }

     

     if (Input.GetKeyDown(KeyCode.Space))
     {
         Jump();
     }
     
 
 }

 public void Jump()
 {
     if (grounded)
     {
         
         rb.AddForce(player.transform.up * jumpForce);
         player_anim.SetBool("Jump", true);
         grounded = false;
     }
     
 }

  void OnCollisionEnter(Collision other)
 {
    if (other.gameObject.tag == "ground" || other.gameObject.tag == "Enemy" || other.contacts[0].normal.y < 1.3f && other.contacts[0].normal.y > 0.7f) //last one is any other flat surface
    {
         if(other.contacts[0].normal.y < 1.3f && other.contacts[0].normal.y > 0.7f)
         {
             grounded = true;
             Debug.Log(other.contacts[0].normal);
             player_anim.SetBool("Jump", false);
         }
         
        
    }
 }


}

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 DCordoba · Feb 19, 2020 at 12:47 AM 0
Share

create a physics material, set a low dynamic friction, a bouciness up than 0.1f and set friction combine to minimium, add it to the player colliders and check the result fit your desire, otherwise play a little with the variables to make it more "slippery"

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 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

Related Questions

Velocity remains constant even when colliding 0 Answers

Rigidbody2D velocity changes when moving through a platform collider. 1 Answer

How to move a rigidbody2D at a constant speed without AddForce? 1 Answer

How to make character move up walls and along the ceiling? 0 Answers

Lerp can someone explain please 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges