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 /
  • Help Room /
avatar image
0
Question by SpaceManDan · Sep 27, 2015 at 06:05 AM · physicscharactercontrollercontroller

FPS Walker bunny hops if stop walking while on slopes

Surprised not to see a peep nor word about this issue as it's inherent in all the rigidbody FPS scripts that disable gravity i.e. FPSwalker scripts.

My code is specifically using GravityFPSWalker.

When walking up any slope (the more angle the more it happens), if a player stops input, the gameobject will hop. I assume this is an effect of an object colliding into an angle this steep (Like real physics would treat a capsule colliding at this angle) but it kinda ruins the otherwise awesome motion of this script. moving on slopes is very common in my game and not hopping when stopping on slopes is important to me. Any way anyone can see to solve this issue?

 using UnityEngine;
 
 /* -------------------------------------------------------------------------------
 GravityFPSWalker
 
 This component is added to a GameObject with a RigidBody. It allows the player
 to move the RigidBody using the vertical and horizontal inputs, and to jump
 using the jump button.
 
 The RigidBody is pushed towards its own custom Gravity vector. The body will
 rotate to stay upright with the RotationRate.
 
 This component uses a raycast to determine if the RigidBody is standing on 
 the ground. The GroundHeight variable should be the distance between the
 GameObject transform and a little further than the bottom of the RigidBody.
 
 The LookTransform should be a child GameObject which points in the direction
 that the player is looking at. This could for example be a child GameObject 
 with a camera. The LookTransform is used to determine the movement veectors.
 ------------------------------------------------------------------------------ */
 [RequireComponent(typeof(Rigidbody))]
 public class GravityFPSWalker : MonoBehaviour {
 
 public Transform LookTransform;
 public Vector3 Gravity = Vector3.down * 9.81f;
 public float RotationRate = 0.1f;
 public float Velocity = 8;
 public float GroundControl = 1.0f;
 public float AirControl = 0.2f;
 public float JumpVelocity = 5;
 public float GroundHeight = 1.1f;
 private bool jump;
 
 void Start() { 
     rigidbody.freezeRotation = true;
     rigidbody.useGravity = false;
 }
 
 void Update() {
     jump = jump || Input.GetButtonDown("Jump");
 }
 
 void FixedUpdate() {
     // Cast a ray towards the ground to see if the Walker is grounded
     bool grounded = Physics.Raycast(transform.position, Gravity.normalized, GroundHeight);
 
     // Rotate the body to stay upright
     Vector3 gravityForward = Vector3.Cross(Gravity, transform.right);
     Quaternion targetRotation = Quaternion.LookRotation(gravityForward, -Gravity);
     rigidbody.rotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, RotationRate);
 
     // Add velocity change for movement on the local horizontal plane
     Vector3 forward = Vector3.Cross(transform.up, -LookTransform.right).normalized;
     Vector3 right = Vector3.Cross(transform.up, LookTransform.forward).normalized;
     Vector3 targetVelocity = (forward * Input.GetAxis("Vertical") + right * Input.GetAxis("Horizontal")) * Velocity;
     Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
     Vector3 velocityChange = transform.InverseTransformDirection(targetVelocity) - localVelocity;
 
     // The velocity change is clamped to the control velocity
     // The vertical component is either removed or set to result in the absolute jump velocity
     velocityChange = Vector3.ClampMagnitude(velocityChange, grounded ? GroundControl : AirControl);
     velocityChange.y = jump && grounded ? -localVelocity.y + JumpVelocity : 0;
     velocityChange = transform.TransformDirection(velocityChange);
     rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
 
     // Add gravity
     rigidbody.AddForce(Gravity * rigidbody.mass);
 
     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 SpaceManDan · Sep 27, 2015 at 07:10 AM 0
Share

@cjdev

Help me cjdev.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by cjdev · Sep 27, 2015 at 08:13 AM

Your problem is likely that rigidbodys don't stop immediately once they no longer have force applied, they have to slow down first under the effects of drag and inertia. You might try locking the position when there is no user input to overcome the problem.

Comment
Add comment · Show 2 · 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
avatar image SpaceManDan · Sep 27, 2015 at 08:30 AM 0
Share

I was afraid you might say that. Problem with that is if I do that there isn't a smooth transition to a stop which is why I want to use this in the first place...

avatar image SpaceManDan SpaceManDan · Sep 27, 2015 at 08:32 AM 0
Share

Also just to let you know, I did try that and it does work so you are right about your suggestion. It just doesn't keep with the fluid motion I'm going for.

avatar image
0

Answer by Dorscherl · Jun 21, 2020 at 09:04 PM

As old and irrelevant as this might be for you specifically. I found projecting the velocity change to the ground normal (only when grounded) to be the perfect thing to prevent the rigidbody from launching upward.

 savedMovementVelocity = Vector3.Lerp(savedMovementVelocity, targetMovementVelocity, stableMovementSharpness * deltaTime);
 Vector3 velocityChange = Vector3.ProjectOnPlane(savedMovementVelocity - velocity, character.GroundNormal);
 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);

Best thing is that it doesn't interupt the rigidbody's natural gravity.
If you want to use it for air movement, i would change the ground normal for the character's up direction (transform.up).

I would consider having the ground checking code get an average of everything underneath the player, either using an array of raycast or all contact points below the collider just because you might have a minor launch issue on drastic changes in slopes (ramps) terrain should be fine

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

36 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

Related Questions

Moving a player character that has ragdoll colliders/rigidbodies 0 Answers

i need help in unity self balancing ragdoll 0 Answers

Character Controller too lag and no and without equal height 0 Answers

[RESOLVED] Two rigidbodies not colliding (even with traditional fixes, e.g. raycasting, collision detection) 2 Answers

Physics based Third Person Controller 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