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 MattDUS · Aug 20, 2020 at 09:26 AM · movementrigidbodycharactermovement script

Rigidbody Player Controller sticking to walls

My player controller is a rigidbody so whenever I'm running at a wall my player would get stuck instead of sliding down. Another thing I tried was using a physics material, but that just makes it so my player feels as if it's sliding on ice and is pretty uncontrollable. I'm also pretty new at programming so let me know if there are any improvements I could do to tweak my code, it would be greatly appreciated!

The Code: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlayerController : MonoBehaviour
 {
     [SerializeField]
     private float accelSpeed = 1000;
     [SerializeField]
     private float maxSpeed = 8;
     Rigidbody rb;
     bool grounded = false;
 
     float xV = 0, yV = 0;
     public void Awake()
     {
         rb = GetComponent<Rigidbody>();
     }
     public void Update()
     {
         
         if (rb.velocity.magnitude > maxSpeed) {
             rb.velocity = rb.velocity.normalized;
             rb.velocity *= maxSpeed;
         }
 
         if (Input.GetAxisRaw("Vertical") == 0 && Input.GetAxisRaw("Horizontal") == 0)
         {
             float xVelocity = rb.velocity.x;
             float yVelocity = rb.velocity.z;
             xVelocity = Mathf.SmoothDamp(xVelocity, 0, ref xV, 5);
             yVelocity = Mathf.SmoothDamp(yVelocity, 0, ref yV, 5);
 
             rb.velocity = new Vector3(xVelocity,rb.velocity.y,yVelocity);
         }
         rb.AddForce(transform.forward*Input.GetAxisRaw("Vertical")* accelSpeed * Time.deltaTime + transform.right * Input.GetAxisRaw("Horizontal") * accelSpeed * Time.deltaTime);
        
 
         if (Input.GetKeyDown(KeyCode.Space) && grounded) {
             grounded = false;
             rb.AddForce(Vector3.up*500);
         }
     }
     void OnCollisionEnter(Collision collision)
     {
         if (collision.contacts.Length > 0)
         {
             ContactPoint contact = collision.contacts[0];
             if (Vector3.Dot(contact.normal, Vector3.up) > 0.5)
             {
                 grounded = true;
             }
         }
     }
 }
Comment
Add comment · Show 2
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 Llama_w_2Ls · Aug 20, 2020 at 10:39 AM 0
Share

You could add a directional force away from the wall if you collide with it. This should stop you from sticking to it

avatar image MattDUS Llama_w_2Ls · Aug 20, 2020 at 02:59 PM 0
Share

Yea I tried that but it's more like the player bounces off walls instead of just sliding down, but thanks anyway!

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

276 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Question about movement with Rigidbody | Diagnol movement going faster 2 Answers

Creating a platform game on 2 axis. Should I use a rigid body, or a character controller. 1 Answer

Make character move according to camera position. 1 Answer

character(ball) rolls with less speed when I build it 2 Answers

Character on Moving and Rotating Platform 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