Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 RonanSmithDev · May 03, 2020 at 01:16 AM · movementcharactercontrollermovement scriptcharacter controllercharacter movement

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check

Hey! It's been a while since I've had to ask here but I've searched far and wide, pulling my hair out trying to iron out these bugs from my controller.


I have previously built RigidBody controllers however have decided to try out a Character Controller component based system this time as I don't really need a physics based player for this project, it's just a simple FPS. The code below was taken mostly from a Brackeys video but I have made my own modifications such as usability/readability improvements and also implemented the new input system.


However, I have been having some problems that I can't seem to resolve:

  1. When I walk off an object, the character is pulled down MUCH quicker than if I actually jump from the same object - I believe this is because when I walk from the platform my velocity is already -10 but when jumping it resets to 0 and decreases over time. I don't know how to go about fixing this as the -10 value means that the player isn't accidentally left floating in the event that IsGrounded fires before the player is actually full landed, it also helps stop the jitter when walking down slopes.

  2. When the player pushes up against a wall and jumps, the player jumps and jitters when you hit the ledge, preventing you from completing a jump - I have absolutely no idea why this would be.

  3. I'd rather not be making two .Move() calls in update, not sure about the performance impact but it would make the code more readable; if anyone has any advice on how I can merge these two together without changing the characteristics of the movement i'd be interested, but this isn't too important.


Much appreciate any help, thanks again!


 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     [SerializeField] float runSpeed = 10f;
     [SerializeField] float gravityMultiplier = 2f;
     [SerializeField] float jumpHeight = 1.5f;
 
     InputActions playerInput;
     CharacterController controller;
     LayerMask playerLayer;
     float gravity;
     Vector3 velocity;
 
     void Awake()
     {
         playerInput = new InputActions();
 
         playerInput.Gameplay.Jump.performed += context => Jump();
 
         controller = GetComponent<CharacterController>();
 
         playerLayer = ~LayerMask.GetMask("Player");
 
         gravity = Physics.gravity.y * gravityMultiplier;
     }
 
     void OnEnable() => playerInput.Enable();
 
     void Update()
     {
         if (IsGrounded() && velocity.y < 0)
         {
             velocity.y = -10f;
         }
 
         Vector2 movementInput = playerInput.Gameplay.Move.ReadValue<Vector2>();
 
         gravity = Physics.gravity.y * gravityMultiplier;
 
         Vector3 move = transform.right * movementInput.x + transform.forward * movementInput.y;
         controller.Move(move * runSpeed * Time.deltaTime);
 
         velocity.y += gravity * Time.deltaTime;
         controller.Move(velocity * Time.deltaTime);
     }
 
     bool IsGrounded()
     {
         return Physics.SphereCast(transform.position, 0.4f, Vector3.down, out _, controller.height / 2f, playerLayer);
     }
 
     void Jump()
     {
         if (IsGrounded())
         {
             velocity.y =  velocity.y + Mathf.Sqrt(jumpHeight * -2f * gravity);
         }
     }
 
     void OnDisable() => playerInput.Disable();
 }
 
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

199 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

Related Questions

Player acts weirdly when I release movement input 0 Answers

Character Controller Move in X and Z axis via camera 1 Answer

Help with implementing continuous collision detection 0 Answers

How to prevent character controller from tipping when moving along terrain? 1 Answer

Character Motor - Make it move in Global Space 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