Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 $$anonymous$$imium, 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.

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

CharacterController velocity 1 Answer

Need help for my Arkanoid-like game. i got problem to make my ball reflects on the wall 1 Answer

I want to move the player once while he is jumping 1 Answer

Help With Charchter Controller or Rigidbody . Please! 2 Answers

Velocity from external objects 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