Add Force not working
I have a first person character which i want to make jump. I have a rigidbody and a character controller attached. The rigidbody is set to: Enable gravity - true, is keinematic - false and restrict x and z movement and all rotation. This is my code.
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(Rigidbody))]
public class playerMove : MonoBehaviour {
public float speed = 60.0F;
public float sideSpeed = 40.0F;
public float jumpForce = 80.0F;
public float sensitivity = 30.0F;
public float gravity = 20.0F;
void Update() {
Rigidbody rb = GetComponent<Rigidbody> ();
CharacterController controller = GetComponent<CharacterController> ();
Vector3 forward = transform.TransformDirection (Vector3.forward);
Vector3 sideways = transform.TransformDirection (Vector3.right);
Vector3 up = transform.up;
float curSpeed = speed * Input.GetAxis ("Vertical") * Time.deltaTime;
float curSideSpeed = sideSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime;
float rotation = Input.GetAxis ("Mouse X") * sensitivity * Time.deltaTime;
controller.SimpleMove (forward * curSpeed);
controller.SimpleMove (sideways * curSideSpeed);
transform.Rotate (0, rotation, 0);
if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded) {
rb.AddForce(up * jumpForce);
Debug.Log ("Adding Force");
}
}
}
Any ideas why this is happening?
What is the mass of Rigid Body? Your script doesn't work if mass is set to more than '2'.
Try increasing value of
jumpForce
.$$anonymous$$ake sure
controller.isGrounded
returns true.$$anonymous$$ake sure 'Freeze Position Y' is false.
"Add force not working" - What is not working? Is it the wrong direction? Is the force too small?
"Enable gravity - true, is keinematic - false and restrict x and z movement and all rotation" - Does this mean you set the restrictions of all movement axis and rotation axis to true? Then it can't move.
Are you sure the CharacterController.isGrounded is true? Does it print the log line?
You need to be clear where you have a problem.
Question by TheFish657 · 22 $$anonymous$$utes ago · rigidbody 2d Add Force not working
Is it was 'Rigidbody 2D' or 'Rigidbody'?
it was a regular rigidbody. The debug line prints. isGrounded is set to true. Y axis movement is not restricted. The mass is 1. When i increase jumpforce nothing changes.
CharacterControllers and rigidbodies are usually one or the other -- they tend not to work well together. $$anonymous$$ost CC's jump by manually setting a y-speed for simple$$anonymous$$ove. I can see the idea of limiting the RB to y-only, but still don't trust them together.
I'm also not sure about several simple$$anonymous$$oves in a row. Seems better to combine into one (not sure, but flags may be set by the last one only.)