Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by ragnaros100 · Feb 13, 2012 at 03:21 PM · c#movementrigidbodyjumpfunction

rigidbody.Addforce the force doesnt apply?

I've tried several methods in making a good jump function, but I can somehow not get it right. I've tried to use physics.rigidbody.AddForce...The debug.log works fine and says "SPACE!" But in game the character wont react.

 using UnityEngine;
 using System.Collections;
 
 // Require a character controller to be attached to the same game object
 [RequireComponent(typeof (CharacterController))]
 [AddComponentMenu("Third Person Player/Third Person Controller")]
 
 public class Movement : MonoBehaviour {
 
     public float rotationDamping = 20f;
     public float runSpeed = 10f;
     public int gravity = 20;
     public float jumpSpeed = 8;
     bool canJump;
     float moveSpeed;
     float verticalVel;  // Used for continuing momentum while in air    
     CharacterController controller;
 
     void Start()
     {
         controller = (CharacterController)GetComponent(typeof(CharacterController));
     }
     
     float UpdateMovement()
     {
         // Movement
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
         Vector3 inputVec = new Vector3(x, 0, z);
         inputVec *= runSpeed;
         controller.Move((inputVec + Vector3.up * -gravity + new Vector3(0, verticalVel, 0)) * Time.deltaTime);
     
         // Rotation
         if (inputVec != Vector3.zero)
             transform.rotation = Quaternion.Slerp(transform.rotation, 
                 Quaternion.LookRotation(inputVec), 
                 Time.deltaTime * rotationDamping);
         return inputVec.magnitude;
     }
     
     void FixedUpdate()
     {
         // Check for jump
         if (controller.isGrounded )
         {
             Debug.Log ("you are grounded");
             canJump = true;
             if ( canJump && Input.GetKeyDown("space") )
             {
 
                 Debug.Log ("SPACE!");
                 // Apply the current movement to launch velocity
                rigidbody.AddRelativeForce(transform.up * 2, ForceMode.Impulse);
             }
         }else
         {           
             // Apply gravity
             verticalVel += Physics.gravity.y * Time.deltaTime;
         }
         // Actually move the character
         moveSpeed = UpdateMovement();  
         if ( controller.isGrounded )
             verticalVel = 0f;// Remove any persistent velocity after landing
     }
 }
Comment
Add comment · Show 10
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 ragnaros100 · Feb 15, 2012 at 09:51 PM 0
Share

This is my first question here... How long does it usually take to get an answer around here?

avatar image rabbitfang · Feb 16, 2012 at 05:17 AM 3
Share

To answer your second question, it depends. Some questions never go answered. Some times no one has an answer or your question just gets missed.

To better improve the chances of your question being answered, keep in $$anonymous$$d a few tips:

1) $$anonymous$$ake your question clear. Don't say "It doesn't work!"; say what isn't working about it (be as specific as you can be), e.g., "The ball isn't rolling down the slope!"

2) $$anonymous$$ake code nicely formatted. At $$anonymous$$imum, you should highlight all your code (within Unity Answers) and click on the 1010 button. This formats your code so it is all awesome and monospaced. You should also make your code easy to read. $$anonymous$$eep consistent indentation schemes; avoid unnecessary whitespace/newlines; make variable/function/class names easy to understand; only show relevant code.

3) Tell us what you have done to try to solve your problem so far. People are more willing to help if you have shown that you tried to do it yourself ins$$anonymous$$d of just co$$anonymous$$g to UA as soon as you have an issue.

4) Clearly define what you mean when you use vocabulary specific to your issue (in your case: "jump"; when I first saw the question, I thought the Q was about jump statements in C# (horrible program$$anonymous$$g method)). A lot of people are ego-centric and usually think that people know exactly what they are talking about (it is kind of them talking about that "one TV show with that actor that has that funny scene" and expecting everyone to all say the exact same thing

5) I cannot stress this enough. WHAT EVER YOU DO, UNDER NO CIRCU$$anonymous$$STANCES, DIRECTLY AS$$anonymous$$ FOR SO$$anonymous$$EONE TO GIVE YOU CODE! (e.g. "can someone code this for me?"). That will most likely get you hate answers/comments in response.

(I'm not saying that all this applies to you. You asked a question and I answered)

(Should I convert this to an answer as it is an answer to a question... just not the original question but a question asked by user in a comment)

avatar image mweldon · Feb 16, 2012 at 05:18 AM 0
Share

However long it takes before someone reads this who knows the answer.

avatar image ragnaros100 · Feb 16, 2012 at 10:35 AM 2
Share

now. I've removed the spaces in the code and reformulated the question. Hope this is better :)

avatar image syclamoth · Feb 16, 2012 at 10:43 AM 2
Share

I am so happy with you right now.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rabbitfang · Feb 17, 2012 at 03:17 AM

Since you improved your question and code so much (though the code still has a few style issues)...

CharacterController disables Rigidbody physics on your object (try it; an object with a rigidbody with gravity (just so it is easy to test as it should fall) on won't move with a CharacterController attached even if it is disabled).

So, you have 3 options:

  1. Take out your character controller and use only rigidbody to get your desired behaviors.

  2. Take out rigidbody and try to simulate physics with position moving and velocity modifying.

  3. Complain to Unity Technologies that they should work together.

Number 3 is a little extreme and probably won't get your problem solved any time soon. So go with number 1 or 2.

Which one you pick is dependent on which is more important, the CharacterController or the Rigidbody, and how much can the component behavior be imitated in code. If you only need the Rigidbody for jumping and gravity, I would stick with using the CharacterController.

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Interpolate problem? Jump isn't smooth... 1 Answer

GetButtonDown is unresponsive 0 Answers

Jump Code Not Working (2D C#) 2 Answers

Moving a Rigidbody Relative to Camera Direction 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