Hello, my player is not running down slopes correctly. He is “bouncing” down them almost, and I believe this is as a result of “isGrounded” in the CharacterController. Has anyone got a fix for this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public CharacterController controller;
public float InputX;
public float InputY;
public float speed;
public float RotateSpeed;
public float maxSpeed;
public float maxRunSpeed;
public float acceleration;
public float deceleration;
public bool running;
public float jumpPower;
public float gravity;
public Vector3 moveDirection;
public float groundDetection;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
moveDirection = Vector3.zero;
}
// Update is called once per frame
void Update()
{
InputX = Input.GetAxis("Horizontal");
InputY = Input.GetAxis("Vertical");
//old, doesn't take rotation into account
//moveDirection = new Vector3(InputX, moveDirection.y, InputY);
moveDirection = transform.TransformDirection(new Vector3(InputX, moveDirection.y, InputY));
moveDirection.x *= speed;
moveDirection.z *= speed;
running = Input.GetButton("Fire3");
if(controller.isGrounded)
{
moveDirection.y = 0.0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpPower;
}
}
//moveDirection.y -= gravity * Time.deltaTime;
moveDirection.y -= gravity;
if (InputX != 0 || InputY != 0)
{
speed += acceleration;
if (running == true)
{
if (speed >= maxRunSpeed)
{
speed = maxRunSpeed;
}
}
else
{
if (speed >= maxSpeed)
{
speed = maxSpeed;
}
}
}
else
{
speed -= deceleration;
if (speed <= 0)
{
speed = 0;
}
}
controller.Move(moveDirection * Time.deltaTime);
//Rotate the player on the X axis (originally in camera)
//Set up X input
float MouseX = Input.GetAxis("Mouse X");
//Set up input and rotate if LMB is held down
float LookX = MouseX * RotateSpeed;
if (Input.GetButton("Fire1"))
{
transform.Rotate(0, LookX, 0);
}
}
}
P.S. if this code is bad or hard to read, I am sorry. I’m not very experienced.