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 xthunderduckx · Jul 17, 2021 at 02:04 PM · physics3dcharacter controllerslopes

Character Controller continues moving upwards on slopes after horizontal movement has stopped

I'm one step away from getting the right feel for my character controller- I have an issue that kind of caused me to stop game development for a while but I really want to get it out of the way and move onto the cool stuff in my game.

I've included a video link that demonstrates the problem: https://youtu.be/_WNIqz6dbfQ

As you can see, the player ascends the slope as intended, but once the movement keys are released, the player does a little bit of a "bump" upwards, falls back down, and if you watch closely, you'll see that they also slowly ascend upwards for a short time before falling back down. If I can eliminate this bump and the little bit of levitation at the end I will be happy with what I have.

 using UnityEngine;
 
 public class moveTest : MonoBehaviour
 {
     //Essentials
     [SerializeField] private GameObject playerObject;
     [SerializeField] private GameObject playerModel; 
     [SerializeField] private Camera playerCamera;
     private CharacterController playerController;
     
 
     private Vector3 playerVelocity; 
     private Vector3 moveDirection; 
 
     private float minimumSpeed = 0.1f;
     private float gravityForce = 25f; 
     private float terminalVelocity = -25f; 
     private bool grounded = true; 
     private float accelerationSpeed = 100f;   
     float frictionOffset = 94.42f; 
 
 
     LayerMask playerCollisionLayer = (1 << 6) | (1 << 7) | (1 << 0);
 
     double mouseSensitivity = 1;
     bool invertY = true;
 
     private int fieldOfView = 90; 
 
     KeyCode moveForward = KeyCode.W;   
     KeyCode moveBack = KeyCode.S;   
     KeyCode moveLeft = KeyCode.A;   
     KeyCode moveRight = KeyCode.D;   
 
     KeyCode lookUp = KeyCode.UpArrow;   
     KeyCode lookDown = KeyCode.DownArrow;   
     KeyCode turnLeft = KeyCode.LeftArrow;   
     KeyCode turnRight = KeyCode.RightArrow; 
 
     KeyCode crouch = KeyCode.X;
     KeyCode jump = KeyCode.Space;
     KeyCode use = KeyCode.E; 
     KeyCode flashLight = KeyCode.L; 
 
     KeyCode primaryFire = KeyCode.Q; 
     KeyCode secondaryFire = KeyCode.Mouse1; 
     KeyCode ability = KeyCode.F; 
     KeyCode ability2 = KeyCode.V; 
     KeyCode ability3 = KeyCode.C; 
 
     KeyCode reload = KeyCode.R; 
 
     KeyCode console = KeyCode.Tilde; 
     KeyCode quickSave = KeyCode.F6; 
     KeyCode quickLoad = KeyCode.F9; 
     KeyCode openChat = KeyCode.Slash; 
     KeyCode levelView = KeyCode.Minus; 
 
     
     
     private void controlMouse()
     {
         float mouseX = Input.GetAxisRaw("Mouse Y");
         float mouseY = Input.GetAxisRaw("Mouse X"); 
         
         if (Input.GetKey(lookUp)) 
         {
             mouseX -= 90f * Time.deltaTime;
         }
         if (Input.GetKey(lookDown))
         {
             mouseX += 90f * Time.deltaTime;
         }
         if (Input.GetKey(turnLeft))
         {
             mouseY -= 90f * Time.deltaTime;
         }
         if (Input.GetKey(turnRight))
         {
             mouseY += 90f * Time.deltaTime;
         }
 
         playerModel.transform.eulerAngles += new Vector3(0, mouseY, 0);
         playerCamera.transform.eulerAngles += new Vector3(mouseX, mouseY, 0);
     }
 
     private void handleInput() 
     {
 
 
         moveDirection = Vector3.zero; 
 
         
         if (Input.GetKey(moveForward))
         {
             moveDirection += playerModel.transform.forward;
         }
         if (Input.GetKey(moveBack))
         {
             moveDirection -= playerModel.transform.forward;
         }
         if (Input.GetKey(moveLeft))
         {
             moveDirection -= playerModel.transform.right;
         }
         if (Input.GetKey(moveRight))
         {
             moveDirection += playerModel.transform.right;
         }
 
         if(Input.GetKey(jump) && isPlayerGrounded() && playerVelocity.y < 10f)
         {             
             playerVelocity.y = 10f;
         }
 
         if (Input.GetKey(crouch))
         {
             //not yet implemented.  
         }
         moveDirection = moveDirection.normalized;
         
         playerVelocity += moveDirection * accelerationSpeed * Time.deltaTime; 
 
         if (Input.GetKey(use))
         {
 
         }
         playerController.Move(playerVelocity * Time.deltaTime);
     }
 
     
     private void applyPhysics() 
     {
         Vector3 tempVelocity = playerVelocity;
         tempVelocity.y = 0;
 
         tempVelocity *= Time.fixedDeltaTime * frictionOffset;
 
         playerVelocity.x = tempVelocity.x;
         playerVelocity.z = tempVelocity.z;
         if (playerVelocity.y > terminalVelocity) 
         {
             if(!isPlayerGrounded())
             {
                 playerVelocity.y -= Time.fixedDeltaTime * gravityForce;
             }
             if(isPlayerGrounded())
             {
                  //not sure what to do here- something could be placed here to eliminate the bump.  
             }
 
         }
 
 
         if (playerVelocity.z < minimumSpeed && playerVelocity.z > -minimumSpeed) { playerVelocity.z = 0; }
         //if (Mathf.Abs(playerVelocity.z) < minimumSpeed ) { playerVelocity.z = 0; }
         if (playerVelocity.x < minimumSpeed && playerVelocity.x > -minimumSpeed) { playerVelocity.x = 0; }
     }
 
 
     private bool isPlayerGrounded()
     {
         Vector3 center = new Vector3(this.transform.position.x, this.transform.position.y - 1f, this.transform.position.z);
         Vector3 size = new Vector3(0.35f, 0.1f, 0.35f);
 
         if (Physics.CheckBox(center, size, new Quaternion(), playerCollisionLayer))
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
 
 
 
     private void OnControllerColliderHit(ControllerColliderHit collision) 
     {
         Vector3 collisionDifference = collision.normal * Vector3.Dot(playerVelocity, collision.normal);
         playerVelocity -= collisionDifference;
     }
 
 
     void Start()
     {
         //Application.targetFrameRate = 30;
 
         playerObject = this.gameObject;
         playerController = playerObject.GetComponent<CharacterController>();
         playerCamera = playerObject.GetComponentInChildren<Camera>();
     }
 
     private void FixedUpdate()
     {
         applyPhysics();
     }
 
     void Update()
     {
         print(playerVelocity);
 
         controlMouse();
         handleInput();
     }
 }


I feel like I've tried everything there is to try- why is this happening and what can I change in my code in order to prevent it / fix it?

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
Best Answer

Answer by xthunderduckx · Jul 18, 2021 at 01:44 AM

Solved- tripled the force of gravity whilst the player is grounded- not only does this stop the vertical momentum from carrying the player upwards, but it also has the side effect of being a slope limiter- ie that the player can not climb ramps above a certain angle without sufficient speed.

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

228 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

Related Questions

Check if the normal of a collision is within a range of angles 2 Answers

Performance Physics.Checkbox/CheckSphere vs Collider/Trigger 1 Answer

Gun script using Physics.Raycast not working? 2 Answers

3D Hook With Swing Physics 0 Answers

CharacterController.Move uses gravity even though it shouldn't 1 Answer


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