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 Bilal_Aljandly · Feb 15 at 08:48 AM · rigidbodyplayer movement

rigidbody based character controller

Hi! I makeíng a charater controller rigidboy based! The movement is all great but not when the player tries going up/down slops. When the player walk on a slop, the Y axies velocity goes crazy

Here is my code:

 [Header("Needed Components")]
     [SerializeField] Rigidbody playerRb;
     [SerializeField] CapsuleCollider playerCollider;
 
     [Header("Movement")]
     [SerializeField] float walkingSpeed;
     [SerializeField] float runningSpeed;
 
     [Header("Jump")]
     [SerializeField] float jumpForce;
     [SerializeField] GroundChecking groundChecking;
 
     [Header("Stair Climbing")]
     [SerializeField] LayerMask layerFilter;
     [SerializeField] GameObject lowerRay;
     [SerializeField] GameObject upperRay;
     [SerializeField] float stepHieght;
     [SerializeField] float stepSmooth;
 
     // Input
     private MovementInputManger playerInput;
 
     private void OnValidate()
     {
         // Stair Climbing
         upperRay.transform.localPosition = new Vector3(upperRay.transform.position.x, stepHieght, upperRay.transform.position.z);
     }
 
     private void Start()
     {
         playerInput = MovementInputManger.Instance;
 
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 
     private void Update()
     {
         if (playerInput.PlayerJumpedThisFrame())
             Jump();
     }
 
     private void FixedUpdate()
     {
         Move();
     }
 
     private void Move()
     {
         float movingSpeed;
         if (playerInput.IsRunning())
             movingSpeed = runningSpeed;
         else
             movingSpeed = walkingSpeed;
 
         playerRb.velocity = Vector3.up * playerRb.velocity.y // Y velocity
                             + playerCollider.transform.right * movingSpeed * playerInput.GetPlayerMovement().x // X velocity
                             + playerCollider.transform.forward * movingSpeed * playerInput.GetPlayerMovement().y; // Z velocity
 
         if (playerRb.velocity.z != 0f || playerRb.velocity.x != 0f)
             stepClimb();
     }
 
     private void Jump()
     {
         if (groundChecking.IsGrounded())
         {
             playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
         }
     }
 
 
     bool hitLower, hitUpper;
     private void stepClimb()
     {
         if (Physics.Raycast(lowerRay.transform.position, playerCollider.transform.forward, out RaycastHit lowerHit, playerCollider.radius + 0.2f, layerFilter))
         {
             hitLower = true;
             if (!Physics.Raycast(upperRay.transform.position, playerCollider.transform.forward, out RaycastHit upperHit, playerCollider.radius + 0.3f, layerFilter))
             {
                 playerRb.position += new Vector3(0f, stepSmooth, 0f);
                 hitUpper = true;
             }
             else hitUpper = false;
         }
         else 
         { 
             hitLower = false; 
             hitUpper = false;
         }
     }
 
     private void OnDrawGizmos()
     {
         if (hitLower)
         {
             Gizmos.color = Color.green;
             Gizmos.DrawLine(lowerRay.transform.position, lowerRay.transform.position + playerCollider.transform.forward * (playerCollider.radius + 0.2f));
         }
         else
         {
             Gizmos.color = Color.red;
             Gizmos.DrawLine(lowerRay.transform.position, lowerRay.transform.position + playerCollider.transform.forward * (playerCollider.radius + 0.2f));
         }
 
 
         if (hitUpper)
         {
             Gizmos.color = Color.green;
             Gizmos.DrawLine(upperRay.transform.position, upperRay.transform.position + playerCollider.transform.forward * (playerCollider.radius + 0.3f));
         }
         else
         {
             Gizmos.color = Color.red;
             Gizmos.DrawLine(upperRay.transform.position, upperRay.transform.position + playerCollider.transform.forward * (playerCollider.radius + 0.3f));
         }
 
     }

I am kind sure the issue in the move function where i am setting the playerRb velocity, but i have no clue how to fixe it.

I will be greatfull for any help.

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

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

188 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

Related Questions

Player keeps falling halfway through terrain floor 3 Answers

Stop rigidbody player from juddering when walking into a wall. 0 Answers

Trying to make a 2d object launch towards the mouse when I left click. can anyone help? 2 Answers

Player is Speeding up in collisions 0 Answers

5.4 broke rigidbody.addForce? 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