My player jumps when going up a slope, but he must walk straight without jumping and he sliding when staying on the slope. how to fix that?
Here is the player movement code:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
private CapsuleCollider player;
[Header(“Floats”)]
public float speed = 7f;
public float Forcejump = 5f;
public float runspeed = 13f;
public float height = 0.5f;
public float crouchspeed = 3f;
[Header(“Bools”)]
public bool isGrounded = false;
public bool isSprinting = false;
public static bool isCrouching = false;
private void Start()
{
rb = GetComponent<Rigidbody>();
player = GetComponent<CapsuleCollider>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.C) && isSprinting)
{
player.height = height;
Invoke("Sliding", 0.7f);
}
Ray ray = new Ray(transform.position, -transform.up);
if (Physics.Raycast(ray, 1.3f))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded && !isCrouching)
{
rb.AddForce(Vector3.up * Forcejump, ForceMode.Impulse);
}
if (Input.GetKeyDown(KeyCode.C) && !isSprinting)
{
isCrouching = true;
player.height = height;
}
if (Input.GetKeyUp(KeyCode.C) && !isSprinting)
{
isCrouching = false;
player.height = 2;
}
}
private void FixedUpdate()
{
float v = Input.GetAxis("Vertical") * speed;
float h = Input.GetAxis("Horizontal") * speed;
float runV = Input.GetAxis("Vertical") * runspeed;
float runH = Input.GetAxis("Horizontal") * runspeed;
float crouchV = Input.GetAxis("Vertical") * crouchspeed;
float crouchH = Input.GetAxis("Horizontal") * crouchspeed;
if (Input.GetKey(KeyCode.LeftShift) && !isCrouching)
{
isSprinting = true;
rb.velocity = transform.TransformDirection(new Vector3(runH, rb.velocity.y, runV));
}
else if (isCrouching)
{
rb.velocity = transform.TransformDirection(new Vector3(crouchH, rb.velocity.y, crouchV));
}
else
{
rb.velocity = transform.TransformDirection(new Vector3(h, rb.velocity.y, v));
isSprinting = false;
}
}
void Sliding()
{
isCrouching = false;
isSprinting = false;
player.height = 2;
}
}