I'm working on a basic movement system for the player in a 3D Box platformer and for a month I've been having problems with the double jump system. The one bug that I still can't figure out is whenever I press the jump button, it has a force that is also pushing the box to the negative direction of the x-axis.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
[SerializeField] private float force;
[SerializeField] public float jumpForce;
float doubleJumpCount = 0;
bool doubleJump;
bool jump;
bool forward;
bool backwards;
bool left;
bool right;
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.name == "Floor")
{
doubleJumpCount = 0;
}
}
// Update is called once per frame
void Update()
{
jump = Input.GetButtonDown("Jump");
left = Input.GetButton("Left");
right = Input.GetButton("Right");
forward = Input.GetButton("Up");
backwards = Input.GetButton("Down");
if(jump)
{
Debug.Log("doubleJumpCount = " + doubleJumpCount);
}
}
void FixedUpdate()
{
if(right) rb.AddForce(Vector3.right * force * Time.deltaTime);
if(left) rb.AddForce(Vector3.left * force * Time.deltaTime);
if (forward) rb.AddForce(Vector3.forward * force * Time.deltaTime);
if (backwards) rb.AddForce(Vector3.back * force * Time.deltaTime);
if (jump)
{
Jump();
}
}
void Jump()
{
if(doubleJumpCount >= 2)
{
return;
}
else
{
rb.velocity = Vector3.zero;
rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
doubleJumpCount++;
}
}
}
I have a suspicion that it might have something to do with the settings i have set for the jump button in the project settings. Or maybe should I use the transform class instead of using a RigidBody3D class to manipulate movement?