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 Classic_soul · Mar 15, 2020 at 05:19 AM · charactercontroller

How do you stop counter movement while in the air?

Example of the bug I'm having: https://youtu.be/w6tAEo2d6UI

I'm having a bug in my project that I've tried so long to figure out. My character movement script is based off the script Dani made on his tutorial channel: https://www.youtube.com/watch?v=XAC8U9-dTZU.

I've added sticking and leaping to the script. My big problem is when I leap:

 void Leap()
 {
     float leapThisFrame = leapAmount;
     rb.AddForce(LeapDirection.transform.forward * leapThisFrame);
 }

I find the counter movement function in my script and as you can see if the player is not grounded or is jumping counter movement should be disabled.

 private void CounterMovement(float x, float y, Vector2 mag)
 {
 if (!grounded || jumping) return;


     //Slow down sliding
     if (crouching)
     {
         rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideCounterMovement);
         return;
     }

     //Counter movement
     if (Math.Abs(mag.x) > threshold && Math.Abs(x) < 0.05f || (mag.x < -threshold && x > 0) || (mag.x > 
     threshold && x < 0))
     {
         rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * -mag.x * 
     counterMovement);
     }
     if (Math.Abs(mag.y) > threshold && Math.Abs(y) < 0.05f || (mag.y < -threshold && y > 0) || (mag.y > 
     threshold && y < 0))
     {
     rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * -mag.y * 
     counterMovement);
     }

     //Limit diagonal running. This will also cause a full stop if sliding fast and un-crouching, so             
     not optimal.
     if (Mathf.Sqrt((Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2))) > maxSpeed)
     {
         float fallspeed = rb.velocity.y;
         Vector3 n = rb.velocity.normalized * maxSpeed;
         rb.velocity = new Vector3(n.x, fallspeed, n.z);
     }

But it seems that there is a very slight delay and when I try to leap my character starts leaping then somehow touches the ground again before going into the air and just stops the leap mid air. I've tried to transform the player up a little then leap but it still is so fast that the script still thinks the character is grounded. I can get around that by putting in an invoke but then the leap is delayed and it feels very choppy. I have also tried to set grounded = false in the leap function and that does nothing. You can see in the video on the right the bool for leaping is always set to false even if the character is leaping. I can also delete counter movement in the script and just use unity's friction but that makes it so I can't go up slopes or slide.

I am just trying to see what I can do to make it so there is not counter movement when I'm leaping! Definitely if you guys have any ideas to try let me know.

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 antounk · Dec 30, 2020 at 08:55 AM

hello, from what i can tell the player becomes grounded after he has counter movement applied; it's a timing error i think so:

try putting the ground check function before you're applying movement

since you're using dani's character controller, for that to work, instead of using OnCollisionStay to check if your player is grounded, do a normal ground check:

 void handleGrounded()
 {
     if (Physics.CheckSphere(groundCheck.position, GroundDistance, whatIsGround))
     {
         isGrounded = true;
     }
     else
     {
         isGrounded = false;
     }
 }



and then to check before applying movement:

 void Update() // can be FixedUpdate
 {
     handleGrounded();
     Movement(); 
     
 }







hope that helped, and goodluck in the game you are working on

Comment
Add comment · Show 1 · 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 antounk · Dec 30, 2020 at 08:57 AM 0
Share

oh btw, GroundDistance is a float, and groundCheck is a transform close to the player's feet

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

215 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

Related Questions

I can't rotate and move in the opposite direction with character Controller 0 Answers

How to move my rigid body in the direction my model is facing. 0 Answers

Gravity To CharacterController 0 Answers

Question on Unity5 CharacterController performance/efficiency 0 Answers

Making a 2D platformer, where am I going wrong? 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