- Home /
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
}
}
This is my first question here... How long does it usually take to get an answer around here?
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)
However long it takes before someone reads this who knows the answer.
now. I've removed the spaces in the code and reformulated the question. Hope this is better :)
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:
Take out your character controller and use only rigidbody to get your desired behaviors.
Take out rigidbody and try to simulate physics with position moving and velocity modifying.
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.
Your answer
Follow this Question
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