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 thestrandedmoose · Feb 28, 2019 at 06:25 PM · rigidbodygravityslope

Preserve horizontal speed on slopes

Hey guys I have a 3D character controller with rigidbody that moves along a grid. Basically, if you swipe right, the character will move right until he hits a wall. I also have gravity so my player can jump.

The issue is, when my player climbs a slope or stairs, he slows down drastically, because gravity is pulling him down. Is there a way to preserve my horizontal speed even on upward slopes?

Here is my movement code

 //If we are not moving...
             if (!isMoving)
             {
                 //If requested direction is valid, set it to currentDir and rotate character
                 if (CheckDirection(nextDirection))
                 {
                     currentDirection = nextDirection;
                     RotateCharacter(currentDirection);
                 }
                 //If we are stopped, calculate the next valid start and end position
                 startPosition = rb.position;
                 endPosition = startPosition + (currentDirection * unitLength);
                 //Round the result so we end up on a whole number coordinate
                 endPosition = new Vector3(Mathf.Round(endPosition.x), Mathf.Round(endPosition.y), Mathf.Round(endPosition.z));
                 isMoving = true;
             }
 
             //If we are moving and the current direction is valid (no walls in the way)
             if (isMoving && CheckDirection(currentDirection))
             {
                 //Move rigidbody towards destination
                 float step = speed * Time.deltaTime;
                 rb.MovePosition(Vector3.MoveTowards(rb.position, new Vector3(endPosition.x, rb.position.y, endPosition.z), step));
             }
             else
             {
                 RotateCharacter(nextDirection);
             }
 
             //Calculate horizontal distance remaining from startpoint to endpoint
             Vector3 distanceRemaining = new Vector3(endPosition.x, 0, endPosition.z) - new Vector3(rb.position.x, 0, rb.position.z);
 
             //If we are extremely close to endpoint, stop
             if (distanceRemaining.sqrMagnitude < float.Epsilon)
             {
                 isMoving = false;
             }
             //If we hit a wall, stop
             if (!CheckDirection(currentDirection))
             {
                 isMoving = false;
             }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WarmedxMints · Feb 28, 2019 at 07:19 PM

A few things which come to mind are do away with Unity's physics and use a raycast system for collisions which applies gravity in the players transform direction. Change the vector of gravity to your players transform.down direction using Physics.gravity which would mean you player may walk on walls though or modify the force when traversing a slope.

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.

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

Ramp for stairs causes player to slide down. 1 Answer

Why reducing material friction doesn't make static rigidbody move with gravity 1 Answer

Instantiated prefab changes gravitational speed when dragged? 0 Answers

Raycasting ray go through object with gravity enabled 0 Answers

Turret floats away and pushes everything away from it 2 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