Got the following script off the Character Controller doc page and it’s working only sometimes. I disabled rigid body because it was messing with the rotation of the object, making it rotating like wild. Below is the code:
public class PlayerController : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool groundedPlayer;
private float playerSpeed = 2.0f;
private float jumpHeight = 1.0f;
private float gravityValue = -9.81f;
private void Start()
{
controller = gameObject.AddComponent<CharacterController>();
}
void Update()
{
groundedPlayer = controller.isGrounded;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
// Changes the height position of the player..
if (Input.GetButtonDown("Jump") && groundedPlayer)
{
print("Jump button pressed");
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
playerVelocity.y += gravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}
The jumping here only works sometimes. The object is not always considered grounded so when the jump button pressed it’s usually ignored. Though the object is constantly changing state and sometimes actually gets grounded so that jump triggers. Also, all objects are simply set in the scene in 10 by 10 area, with the player object being in 0 0 0 coordinates, yet it moves a bit when I run the game, I feel like that could cause the issue here.
Any suggestions?