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 /
This question was closed Apr 10, 2019 at 11:16 AM by TreyH for the following reason:

Not a new question and several years old.

avatar image
1
Question by ViFFeX360 · Nov 09, 2016 at 02:57 AM · movementmovement scriptmovemomentum

Momentum with Character Controller

Hey guys,

I'm building a simple FPS scene for a demo I am supposed to create for a project. Walking around and rotating and jumping and such are easily achievable by having created a Character Controller with a custom script that controls it.

What I'm looking for though, is some help in understanding how to keep my momentum while midair (while also retaining control via inputs). So if I let go of my input keys right now, the character just stops. Which is fine while on the ground, but not when midair. I'm aware of the isGrounded if statement, so I know I'll have to utilize that to get it to work, but what I need help with specifically is how to -add- my input while maintaining my previous momentum while midair.

Current movement code:

 float hor  = Input.GetAxis("Horizontal");
 float vert = Input.GetAxis("Vertical");
     
 Vector3 speed = new Vector3(hor, 0f, vert);
 speed           = Vector3.ClampMagnitude(speed, 1f);
     
 if (cc.isGrounded) {
     if (Input.GetButtonDown("Jump"))
        velocity = jump;
 } else {
     velocity += Physics.gravity.y * Time.deltaTime;
 }
     
 speed  *= walk;
 speed.y = velocity;
 speed   = transform.rotation * speed;
     
 cc.Move(speed * Time.deltaTime);
 

Thanks very much for any help :) If there's an existing question that asks this specifically, my apologies, I hadn't seen 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

  • Sort: 
avatar image
0

Answer by LukeNukem44 · Apr 10, 2019 at 10:55 AM

Hello. I had this problem for a while, some time ago. There's no solution for it because there's no relation between moving a Transform or Rigidbody and Physics momentum.


So I actually came up with an alternative after a lot of testing. It's really quite simple, but manual. At least it's simple. LoL


Here's what I did: I made it so that when I jump, I use values which are also manipulated by my walk speed (so that it can vary with varying walk speeds automatically), and with that values, I AddRelativeForce in the direction I've already moving in on the ground based on what move keys I'm holding down. I shortcut it by directly taking the WASD axis values. -- Then, I noticed it wasn't really working still (things rarely do in Unity), so I had to make my jump an AddRelativeForce as well. -- The third and final thin you'll want to do is, if two connecting move pointers, eg: W and D, are held down, then you are walking and therefore jumping at a diagonal. You'll want to change the speed to be multiplied by something like 0.75f - 0.8f for the walk speed and the directional jump force, like forward + right, or reverse + left. The upwards jump force never has to alter base on all of this.


It's been a few years since you've posted this, so if you have a grander alternative, I'd love to learn more. -- If you've created a player character that doesn't repeatedly bounce in walks (one of my current problems (I'm dying here)), but rather slides along them as we should, I'd love to learn from you.


Luke

Comment
Add comment · Show 9 · 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 TreyH · Apr 10, 2019 at 11:15 AM 0
Share

Since OP was controlling their character by manipulating velocity manually, the quick solution was to just have the inputs be dependent on the controller being grounded. Velocity changes would then only happen on the ground and the character would naturally keep momentum in the air.

avatar image LukeNukem44 TreyH · Apr 10, 2019 at 01:00 PM 0
Share

I still don't know what OP is.


Right, there's a lot wrong with doing that, which I got myself through. $$anonymous$$aking them dependent on being grounded negates your following statement, unless you were adding force, which creates instability. I went through them all.

avatar image emilylena · Apr 10, 2019 at 10:36 PM 1
Share

Hi, this was actually me (now on a different account) who asked this question.

I actually did figure out a way to "fake" velocity with a Character Controller, rather than using a Rigidbody with real velocity. I can post the script below for how I did it for just the movement:

 input = new Vector3(Input.GetAxis("Horizontal"), -0, Input.GetAxis("Vertical")); // Get input
 input = transform.TransformDirection(input); // $$anonymous$$ake sure input is applied to the forward direction of the characterController, not the forward direction of the World
 input = Vector3.Clamp$$anonymous$$agnitude(input, 1f); // clamp input to max 1, so you can't have two buttons pressed like W and A for more speed diagonally
 
 velocity.x += input.x * acceleration; // velocity buildup on the horizontal axis
 velocity.z += input.z * acceleration; // velocity buildup on the vertical axis
 
 if (input.x == 0) // if there's no input on the horizontal axis
        velocity.x = $$anonymous$$athf.SmoothDamp (velocity.x, 0f, ref xvelocity, friction); // smoothdamp to zero
 if (input.z == 0) // if there's no input on the vertical axis
        velocity.z = $$anonymous$$athf.SmoothDamp (velocity.z, 0f, ref zvelocity, friction); // smoothdamp to zero
 
 velocity = Vector3.Clamp$$anonymous$$agnitude (velocity, maxSpeed); // clamp the speed so the character doesn't accelerate forever


This works really well for me personally, and keeps momentum when you go in air :) I have no idea how "efficient" this is, but it's basically based on the "high level" idea of how the Sonic franchise handled acceleration back in the SEGA days ^^ Hope it helps :)

avatar image LukeNukem44 emilylena · Apr 11, 2019 at 09:33 AM 0
Share

$$anonymous$$an! That is awesome! $$anonymous$$any thanks for that!


I've got it all happening except, I'm just wondering what your move method is, like: transform.Translate(velocity); or transform.position etc.


This is especially great because I've always got Unity's Character Controller in the back of my $$anonymous$$d tempted to use it because I was fascinated to learn that it is done without physics. -- This now make me wonder if you still use a Rigidbody for an easy Gravity solution. ? -- Then there's the IsGrounded dilemma which I think I made a great solution to.

avatar image emilylena LukeNukem44 · Apr 11, 2019 at 12:34 PM 1
Share

I actually use characterController.$$anonymous$$ove for my movement method, sorry that I didn't include that. This code is specifically in relation to be used for the CharacterController (at least, that's how I made it) though you can probably apply it through Translate or transform.position, but not sure how you'd do that well ^^;

So no Rigidbody, just the CharacterController and my own gravity implementation, though that has still a lot of problems so I won't share that here since I need to fix it first.

IsGrounded works well, as long as you make sure the characterController gets pushed to the ground by a small gravity force when you're not in the air, and then a normal gravity force when you're in the air. I haven't been able to get that to work well on slopes tho, hence why I am still working on that.

Show more comments
Show more comments
avatar image Filters emilylena · Jan 13, 2021 at 02:53 AM 0
Share

love you for this but what are you using for the smooth damp vector3 references name xvelocity and zvelocity?

Follow this Question

Answers Answers and Comments

73 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

Related Questions

Character rotates left and right instead of going forward. 1 Answer

Slowly move a GameObject on 1 axis, then destroy it. 1 Answer

New to Unity & Scripting, How Do I Create a Movement System Thats About Building Up Speed? 0 Answers

How to I put a speed limit on my character's movement? 1 Answer

Implementing Counter-Movement 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