• 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
1
Question by nathanlink169 · Jun 27, 2017 at 10:04 PM · movement3d

More Advanced Player Movement

Hi All,

I've seen countless posts about player movement which has to do with how to move a play, generally asked by newcomers (which is good, we were all newcomers once!)

What I'm looking for how should a player move, in a more advanced sense, and specifically in Unity 3D. This is a little bit of an opinion question, but there are some very objective things that should be avoided, and things that will improve most games.

For example, the basic movement scripts tend to get the player to straightup stop when they player releases the input. That doesn't feel great to me.

So, all in all: What are some tips for making good movement, and what are some things to avoid, in a high level sense (no code samples required)?

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

Answer by Major · Jun 28, 2017 at 05:11 AM

I'm not an expert, but I'll mention some of the things I've learned.

When I make my controllers, I use rigidbodies primarily and use the input from the keyboard, to create a direction vector which is then applied directly to move the character. One of the nice things about this, is that it makes it very easy to set a speed. This may be extremely common, I don't really know.


Normalized Inputs

One of the biggest things that gets on my nerves, is when I see non-normalized inputs. Here's what I mean:

 Vector2 dirVector = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));

versus:

 Vector2 dirVector = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical")).normalized;

If input is not normalized, the character will move faster when moving diagonally. You can solve for the direction vector by adding the magnitudes of the horizontal and vertical vectors and applying the pythagorean theorem. For example, if you are moving diagonally without a normalized input, the magnitude of the direction vector will equal this:

 magnitude = sqrt (1^2 + 1^2), 
 sqrt (2) = 1.41421356237... 

Whenever you are applying input, you must always ensure that the input magnitude is equal to 1. Also, as far as smoothing player input, use pre-filtered input modes, in other words using GetAxis over GetAxisRaw. GetAxisRaw returns either -1, 0, or 1, while GetAxis is pre-filtered and will give a smooth transition.

By storing the input as a vector, you can use the input directly as your direction vector for movement. I find this is cleaner, and more direct than checking input directions using conditional statements.


Moving the Player

This is very dependent on the style of controller that you are making, but in my opinion (for rigidbody controllers) Rigidbody.MovePosition is the best method for moving the character. When it comes to using vectors to move characters, like I mentioned above, I personally find using this method the most robust. I've created controllers that uses Rigidbody.AddForce, but I always find that they tend to get messy and require more code than a controller using Rigidbody.MovePosition. In addition, I find that they tend to be less controllable and less responsive.

Using the above section for reference, you can setup very basic player movement in 2 lines of code:

 Vector3 dirVector = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")).normalized;
 GetComponent <Rigidbody> ().MovePosition (transform.position + dirVector * Time.deltaTime);


Proper Gravity

One of the things I have seen many times when applying gravity to a rigidbody player, is this:

 Rigidbody.AddForce (-transform.up * gravity);

This is physically flawed because this is not how gravity works. Here's what isn't included in the above example: FORCE MODE

By default, AddForce applies a force using ForceMode.Force. This just applies a certain number of newtons to the player. Gravity, however, doesn't work quite in the same way. To think about it, gravity applies a force per kilogram. For example, take the equation F = ma, you know that really famous one from physics class, the force on the object is equal to gravitational acceleration, MULTIPLIED by the mass of the object. So, to properly apply gravity to a character controller when using rigidbodies, ForceMode.Acceleration needs to be included to properly add force to the player:

    Rigidbody.AddForce (-transform.up * gravity, ForceMode.Acceleration);

The full list of ForceModes can be found here.


"Proper" Jumping

A recent thing I've come across in my own code is jumping. I've always had trouble getting the player to jump a specific height every time. In addition, if I wanted to change the player mass, it has always been slightly inconvenient to change the jump force. So instead, I looked back into ForceModes. One of the modes is ForceMode.VelocityChange. This will: "Add an instant velocity change to the rigidbody, ignoring its mass." Using physics energy equations, potential energy and kinetic energy, it is very easy to solve for the exact velocity needed to reach a certain jump height:

 mgh = 1/2mv^2,
 2gh = v^2,
 v = sqrt (2gh). 

So with relative ease, I can set the exact velocity needed to reach my jump height:

 Rigidbody.AddForce (transform.up * sqrt (2gh), ForceMode.VelocityChange);



Grounding the Player

One of the things that took me a while to learn is, the importance of skin width when detecting when the player is grounded with a raycast. All this means is starting the origin of your raycast (or linecast) slightly inside the collider bounds of your player. This vastly improves the consistency of determining if your character is grounded (especially on slopes).


These are the 5 biggest things I've learned when creating controllers. Everybody has their own opinions, but I feel that these methods produce the most consistent results, while also being very customization and friendly to nearly any situation.

Comment
Add comment · Show 4 · 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 KillerHawk · Jan 05, 2019 at 08:49 AM 0
Share

I know this was posted a year ago, but thank you for such a well thought out reply. You have no idea how much this helped me understand character controllers. I have always had a problem with understand these controllers but you really helped, so thanks!

avatar image gameDevMode · Dec 05, 2019 at 08:37 PM 0
Share

I didn't knew the Input Thing! Thank you so much!

avatar image Gemesil · Feb 20, 2020 at 06:51 PM 0
Share

Thank you so much, this helped me immensely.

avatar image Gwynbleidd_27 · Apr 07 at 12:01 PM 0
Share

Thank you so much! I never got a rigidbody controller to work, now I did it with your help thanks a lot <3

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 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

Related Questions

Play animation when WASD is pressed, stop animation when none of them are pressed (Help!!) 0 Answers

how to stop my gun from moving further away from the camera when I look up and down? 0 Answers

Determine the direction an object is actually moving? 0 Answers

Keep position of CharacterController relative to rotating platform 0 Answers

Make the Player unable to move to opposite direction 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