Hey so here is my code for now i am working an an FPS game i got the mechanics down still need the wallrunning and crouching.
My issue is, when i fall down i want the player to fall and increase the speed at which he falls at till he hits the ground, same thing with bhopping or when sliding down a slope.
I just cant get it figured out, if you can help by any way i would really appreciate that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
float playerHight = 2f;
[SerializeField] Transform orientation;
[Header("Movement")]
public float moveSpeed = 6f;
public float jumpForce = 5f;
public float jumpCoolDown = 0.25f;
public float angularSpeed;
float nextJumpTime;
[Header("Drag")]
public float groundDrag = 6f;
public float airDrag = 2f;
[Header("Keybindes")]
[SerializeField] KeyCode jumpKey = KeyCode.Space;
//To multiply the movement
[SerializeField] float airMultiplier = 0.3f;
float movementMultiplier = 10f;
float horizontalMovement;
float verticalMovement;
[Header("Ground Detection")]
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask whatIsGround;
[SerializeField] float groundDistance = 0.2f;
bool isGrounded;
//Vectors
Vector3 moveDirection;
Vector3 slopeMoveDirection;
//RB
Rigidbody rb;
RaycastHit slopeHit;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void Update()
{
//Check if player is grounded
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, whatIsGround);
MyInput();
ControlDrag();
//If player is grounded and space is pressed we can jump
if (Input.GetKey(jumpKey) && isGrounded && Time.time >= nextJumpTime)
{
Jump();
}
slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
}
private bool OnSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHight * 0.5f + 0.5f))
{
//Check if raycast is straight up else its a slope
if (slopeHit.normal != Vector3.up)
{
return true;
}
else
{
return false;
}
}
return false;
}
void MyInput()
{
//Assign the inputs
horizontalMovement = Input.GetAxisRaw("Horizontal");
verticalMovement = Input.GetAxisRaw("Vertical");
//Move in the relative direction where player is looking
moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
}
void Jump()
{
if (isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
//Cooldown for jumping
nextJumpTime = Time.time + jumpCoolDown;
}
}
void ControlDrag()
{
//Reduce drag while not grounded
if (isGrounded)
{
rb.drag = groundDrag;
}
else
{
rb.drag = airDrag;
}
}
//Fixed Update cause we are using a rigidbody
private void FixedUpdate()
{
MovePlayer();
}
void MovePlayer()
{
rb.AddForce(Vector3.down * Time.deltaTime * 10);
if (isGrounded && !OnSlope())
{
//We add .normalized to fix diagonal movement
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (isGrounded && OnSlope())
{
rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (!isGrounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
}
}
}