- Home /
rb.addforce being really buggy
this is supposed to make the character move and jump and double jump,everything was fine but then i had to change the model and i added all the components and then the jump code stopped working. i moved it to the fixed update function and now the gravity doesnt work and when the character moves they are on the ground and when they stop they snap to the y position they started in (i put the character off the ground to test the gravity) i also tried changing the gravity variable but nothings working, please help!
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "level") {
jumpone = false;
jumptwo = false;
}
}
public float speed = 6.0F;
public float jumpSpeed;
public float gravity = 20.0F;
public Rigidbody rgb;
private Vector3 moveDirection = Vector3.zero;
private bool jumpone;
private bool jumptwo;
private void Start()
{
jumptwo = false;
jumpone = false;
}
private void FixedUpdate()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
if (jumpone == true && jumptwo == false)
{
rgb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
jumptwo = true;
Debug.Log("jumping");
}
if (jumpone == false && jumptwo == false)
{
rgb.AddForce(Vector3.up * jumpSpeed,ForceMode.Impulse);
jumpone = true;
Debug.Log("jumping");
}
}
}
Answer by Bunny83 · Jul 27, 2018 at 02:49 AM
You're mixing rigidbody physics with the CharacterController. That doesn't work. The CharacterController uses it's own collision detection and movement method. You should decide if you want a rigidbody based player or a CharacterController based player. Depending on which one you want to use you have to change / remove everything related to the other.
It wouldn't make much sense to show the changes for one of the two if you actually want the other. So you may want to be more clear if you want to have a CharacterController based movement or a rigidbody based movement.
Apart from this you should never check Any XXXDown or XXXUp events in FixedUpdate. This won't work consistently since those input events are only true for one frame and FixedUpdate might not run every frame so you may miss keypresses.
Answer by Zhiyong_Chen · Jul 27, 2018 at 02:32 AM
moveDirection.y -= gravity * Time.deltaTime;
else moveDirection.y -= gravity * Time.deltaTime
Your answer
Follow this Question
Related Questions
how to limit horizontal input when jumping 1 Answer
Mimic realistic gravity for player jump C# 1 Answer
How to drag object faster to the ground? 3 Answers
Player object falling slowly when holding input 2 Answers
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers