- Home /
Why wont my character jump? (using rigidbody and addForce)
Hi all
Ive got what seems to be a quite simple question. For some reason i cannot make my character jump from the ground after i started to implement a logic that would prevent him from doublejumping.
Ive implemented my characters movement like this:
public class PlayerController : MonoBehaviour {
private Vector3 walkSpeed;
private Vector3 jumpHeight;
private float distToGround;
// Use this for initialization
void Start () {
walkSpeed = new Vector3(4, 0, 0);
jumpHeight = new Vector3(0, 12, 0);
distToGround = collider.bounds.extents.y;
}
// Raycast under the player to detect if he is grounded
private bool IsGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
// Player Movement
if (Input.GetAxisRaw("Horizontal") == 1)
{
rigidbody.MovePosition(rigidbody.position + walkSpeed * Time.deltaTime);
}
else if (Input.GetAxisRaw("Horizontal") == -1)
{
rigidbody.MovePosition(rigidbody.position + walkSpeed * -1 * Time.deltaTime);
}
// Player Jumping
if (IsGrounded() && Input.GetButtonDown("Jump"))
{
rigidbody.AddForce(jumpHeight, ForceMode.Impulse);
}
}
}
For some reason (checking with debug.log) my character never clears the 2 if statements checking if he is grounded and if the jump button is pressed.
The weird thing is. If i test on the if statements 1 at a time theyre working just fine.
Can anybody help me?
Answer by AronChan · Nov 06, 2013 at 08:20 PM
It seems the issue resided with the fact that my character had a box collider enveloping him. After having reduced the size of my collider to fit my code it works fine.
Your answer
Follow this Question
Related Questions
Jumping with rigid body3d 1 Answer
Get result (force & torque) of AddForceAtPosition? 2 Answers
Make a player jump tile 1 Answer
AddForce with 2D Character 2 Answers