• 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
Question by SimonDiamond · Mar 04, 2013 at 10:56 AM · movementrigidbodyaddforcejumpingy

Rigidbody.Addforce makes object skip

I'm working on a first person controller and i'm using Rigidbody to make the movement for the controller.

Altough i notice that when i move my character around on a simple plane, it starts skipping(jumping). I don't want to freeze the Y position, because i want my character to be jumping, and go up and down hills ect. Anyone have any tips on making this skipping disappear?

 Direction = new Vector3(0,0,0);
         if (Input.GetKey(KeyCode.W))
         {
             Direction += Vector3.forward;
         }
         if (Input.GetKey(KeyCode.A))
         {
             Direction += Vector3.left;
         }
         if (Input.GetKey(KeyCode.D))
         {
             Direction += Vector3.right;
         }
         if (Input.GetKey(KeyCode.S))
         {
             Direction += Vector3.back;    
         }
         if (Input.GetKey(KeyCode.Space) && CurrentlyJumping == false)
         {
             CurrentlyJumping = true;
         }
             CharSpeed = InputSpeed;
             Direction.Normalize();
             rigidbody.AddRelativeForce (Direction * CharSpeed);

This is the function i'm calling for movement.

Gravity is on by the way

Comment

People who like this

0 Show 4
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 aldonaletto · Mar 04, 2013 at 11:16 AM 0
Share

AddForce should not make the character skip unless the force direction is somewhat upwards - post your code to let us help you.

avatar image SimonDiamond · Mar 04, 2013 at 01:05 PM 0
Share

Alrighty!

avatar image jrussell · Mar 04, 2013 at 09:26 PM 0
Share

Is this code being called within Update() or FixedUpdate() ?

avatar image SimonDiamond · Mar 05, 2013 at 08:37 AM 0
Share

It's being called in Update()

5 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by DavidDebnar · Mar 05, 2013 at 09:32 AM

To fix the skipping, what people usually do is lower the gravity when the character 'is grounded' (has ground under his feet). For this to work, first the gravity on the rigidbody has to be disabled. Second, gravity has to be applied manually.

If you want to make the controller less slidy, play with the drag/speed settings to get the desired result.

My usual rigidbody settings.

Also, make sure to always execute additive Physics functions in the FixedUpdate() function:

 var Direction : Vector3 = Vector3.zero;
 var Speed : float = 5;
 var JumpStrength : float = 5;
 var HalfCharacterHeight : float = 1;
 var DefaultGravity : float = 9.81;
 var GroundedGravity : float = 0.1;
 private var gravity : float = 9.81;
 private var isGrounded : boolean = false;
 
 function Update () {

      //we cast a ray downwards to the characters feet, to check if he's grounded
      isGrounded = Physics.Raycast(transform.position, -transform.up, HalfCharacterHeight);

      //change the gravity setting to Default/Grounded gravity based on the result of the raycast
      if(isGrounded) gravity = GroundedGravity;
      else gravity = DefaultGravity;
 
      Direction = Vector3.zero;
      
      //I'm using unity's input manager instead. You can change this in Edit/P.Settings/Input
      Direction.x = Input.GetAxis("Horizontal"); //you can also change this to 
      Direction.z = Input.GetAxis( "Vertical" ); //GetAxisRaw

      if(Direction.magnitude > 0) //if the direction isn't zero normalize it
           Direction = Direction.normalized; //so that diagonal movement isn't faster

      //if the space key was pressed and the character is grounded, jump!
      if(Input.GetKeyDown(KeyCode.Space) && isGrounded) {
           //using Impulse, to make the jump more realistic               
           rigidbody.AddForce( transform.up*JumpStrength,ForceMode.Impulse);
      }
 }
 
 function FixedUpdate () {
      
      if(Direction.magnitude > 0) //if the direction isn't zero add force
           rigidbody.AddRelativeForce(Direction*Speed); 

      rigidbody.AddRelativeForce(-transform.up*gravity); //apply gravity
 }

You could also try changing the Physics(Fixed) timestep in the Edit/Project Settings/Time menu. This can also solve fast rigidbodies flying trough walls.

Time settings

---David

Comment

People who like this

0 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 SimonDiamond · Mar 05, 2013 at 09:59 AM 0
Share

Thanks for the help! Now i'm no longer using AddForce to move my character but this will help in other situations.

avatar image

Answer by DezBoyle · Jun 18, 2018 at 06:22 PM

Turn on interpolation on your player's rigidbody.

alt text


capture.png (27.3 kB)
Comment
Richmar1

People who like this

1 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 Richmar1 · Apr 27, 2020 at 04:23 PM 0
Share

That Solved It. Thanks!

avatar image

Answer by electricsauce · Mar 04, 2013 at 03:00 PM

You might want to try changing your inputs to:

float h = input.getAxis("Horizontal"); float v = input.getAxis("Vertical"); direction = (h, v, 0);

When you add force with a getbuttondown the full amount of the force is added immediately, but the axis is a little smoother.

Comment
DavidDebnar

People who like this

-1 Show 3 · 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 SimonDiamond · Mar 04, 2013 at 09:22 PM 0
Share

I'll try it out, thanks!

avatar image SimonDiamond · Mar 05, 2013 at 08:56 AM 0
Share

unfortionatly this didn't do much difference, and the GetKeyDown only seems to disable my forward and back. With it i can only go left or right. The cause of the problem is probably behind the skipping aswell.

avatar image SimonDiamond · Mar 05, 2013 at 09:01 AM 0
Share

But the input.getaxis part made my Controller so much better so thanks!

avatar image

Answer by LunaArgenteus · Mar 04, 2013 at 11:11 PM

Check to see if you have more than one collider active on the object - if you've mistakenly done this, the object will push itself / jerk in unintended directions.

Comment

People who like this

0 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 SimonDiamond · Mar 05, 2013 at 08:40 AM 0
Share

I only to have one box collider active, without On Trigger. And the floor i'm walking on making my object skip has a simple Mesh Collider. But that shouldn't be a problem...

avatar image

Answer by SimonDiamond · Mar 05, 2013 at 10:47 AM

Well i guess it's a little embarrassing but i solved the skipping problem by just changing the way i move. I stopped using Rigidbody and simply used transform.translate. With a little help of the nice people answering this question i solved it!

This is the code.

 float h = Input.GetAxis("Horizontal");
         Vector3 Direction = new Vector3(h, 0, 0);
         float CharSpeed = InputSpeed;
         
         if (Input.GetKey(KeyCode.W))
         {
             Direction += Vector3.forward;
         }
         if (Input.GetKey(KeyCode.S))
         {
             Direction += Vector3.back;    
         }
         if (Input.GetKey(KeyCode.A))
         {
             Direction += Vector3.left;    
         }
         if (Input.GetKey(KeyCode.D))
         {
             Direction += Vector3.right;    
         }
         Direction.Normalize();
         transform.Translate(Direction * CharSpeed);

I'm still using Rigidbody for gravity and ect, but the reason why i used AddForce from the start was because i was having problems with the movement adjusting to the camera angle. Hope this will help anyone else who's having similiar problems.

Comment

People who like this

0 Show 5 · 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 LunaArgenteus · Mar 05, 2013 at 11:20 AM 0
Share

The only problem with that is that by using transform.Translate, you are "teleporting" your object and not checking to see if you are hitting anything along the way. Perhaps you might want to consider using a CharacterController.

http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.html

avatar image SimonDiamond · Mar 05, 2013 at 12:11 PM 0
Share

I'm actually trying to create "sort of" my own charactercontroller. That's why i'm not using Unitys CharacterController. But thanks for the advice!

avatar image DavidDebnar · Mar 05, 2013 at 12:52 PM 0
Share

You do realise that by using Translate, you'll also have to write your own physics interaction system. Also, by doing this, you just turned of Y movement, which you said you didn't want to be "freezed", because you want your character to be jumping and going down hills, which won't be happening without a physics engine.

avatar image DavidDebnar · Mar 05, 2013 at 12:56 PM 0
Share

You can also reduce your code to:

 transform.Translate(Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")).normalized*InputSpeed);

That's 22 lines reduced to 1.

avatar image SimonDiamond · Mar 05, 2013 at 01:50 PM 0
Share

I didn't know that actually...i guess i'm going back to AddForce. Thanks for the advice Dávid. I really appreciate it! You're a sun among stars. Or something.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Why is my rigidbody jump height different every time, despite being in FixedUpdate. 1 Answer

[Solved]Why doesn't this Rigidbody.AddForce work the way I tell it to? 1 Answer

Smoother movement and jumping with rigidbodies 1 Answer

Rigidbody falling very slow 1 Answer

Rigidbody movement conflict? 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