I am creating an arena style fps. Players can slide and jump. Sometimes when players hold down the jump button to repeatedly jump each time they touch the ground, or when sliding and jumping, the player does not jump. I added debugs to check if it’s being called and it is. However, the rigidbody isn’t adding any force. Does anyone know why this is?
Also, it is formatted very weirdly do to me attempting to fix this on my own by moving where the actual jump is from in multiple places.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement: MonoBehaviour
{
// the horizontal and vert cam sense
public float horizontalSpeed = 1f;
public float verticalSpeed = 1f;
private float xRotation = 0.0f;
private float yRotation = 0.0f;
public Camera mCam;
//movement stuff
public float speed;
private Rigidbody rb;
public float jumpforce;
public bool grounded;
public bool sliding;
public float slidecounter;
public float slideforce;
public bool jumping;
public bool slideJumping;
void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
jumping = false;
slideJumping = false;
}
void Update()
{
float moveHorizontal = UnityEngine.Input.GetAxisRaw("Horizontal");
float moveVertical = UnityEngine.Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Vector3 moveDir = this.transform.TransformDirection(movement);
rb.AddForce(moveDir.normalized * (speed - slidecounter), ForceMode.Impulse);
if (Input.GetButton("Slide"))
{
gameObject.transform.localScale = new Vector3(1f, 0.75f, 1f);
sliding = true;
rb.AddForce(moveDir.normalized * speed * slideforce, ForceMode.Impulse);
rb.AddForce(Vector3.down * speed * 0.4f, ForceMode.Impulse);
rb.AddForce(speed * Time.deltaTime * rb.velocity.normalized * 0.2f);
}
if (Input.GetButtonUp("Slide"))
{
gameObject.transform.localScale = Vector3.one;
sliding = false;
}
//grav
rb.AddForce(Vector3.down * Time.deltaTime * 9.81f * 30, ForceMode.Impulse);
if (rb.velocity.y < 0f)
{
rb.AddForce(Vector3.down * Time.deltaTime * 9.81f * 45, ForceMode.Impulse);
}
else if (rb.velocity.y > 0f && !Input.GetButton("Jump"))
{
rb.AddForce(Vector3.down * Time.deltaTime * 9.81f * 90, ForceMode.Impulse);
jumpforce = 25;
}
if (grounded)
{
if (Input.GetButton("Jump") && !sliding)
{
jumping = true;
}
else if (Input.GetButton("Jump") && Input.GetButton("Slide"))
{
slideJumping = true;
}
}
if (Input.GetKey(KeyCode.Escape))
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
if (Input.GetKeyUp(KeyCode.Escape))
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
#region
float mouseX = UnityEngine.Input.GetAxis("Mouse X") * horizontalSpeed;
float mouseY = UnityEngine.Input.GetAxis("Mouse Y") * verticalSpeed;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90);
gameObject.transform.eulerAngles = new Vector3(0.0f, yRotation, 0.0f);
mCam.transform.eulerAngles = new Vector3(xRotation, yRotation, 0.0f);
#endregion
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "ground")
{
grounded = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "ground")
{
grounded = false;
}
}
private void FixedUpdate()
{
if (sliding && !grounded)
{
slidecounter += 2.5f * Time.deltaTime;
}
//countermovement
if (!sliding)
{
rb.AddForce(-rb.velocity * speed * 2);
slidecounter = 0;
}
else if (sliding)
{
if (slidecounter <= 8)
{
slidecounter += 4.5f * Time.deltaTime;
rb.AddForce(-rb.velocity * speed * slidecounter);
}
else
{
slidecounter = 8;
}
}
if (jumping)
{
jumpforce = 35;
rb.AddForce(Vector3.up * jumpforce * 10, ForceMode.Impulse);
Debug.Log("it performed a normal jump");
grounded = false;
jumping = false;
}
if (slideJumping)
{
jumpforce = 70;
rb.AddForce(Vector3.up * jumpforce * 10, ForceMode.Impulse);
Debug.Log("it did the thing allegedly");
grounded = false;
slideJumping = false;
}
}
}