using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
//**** WALKSPEED AND NORMALSPEED NEED TO BE THE SAME
public float walkSpeed = 12f;
public float jumpHeight = 100f;
public float sprintSpeed = 18f;
public float moveSpeed = 0f;
public float gravity = -9.81f;
Vector3 velocity;
Vector3 moveDirection;
public Transform groundDetector;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundDetector.position, groundDistance, groundMask);
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//Sprint if player is holding left-shift and "W" at the same time, should return to walking speed when left-shift is let go.
moveDirection = new Vector3(moveX, 0, moveZ);
Debug.Log(moveDirection);
if (Input.GetKeyDown(KeyCode.LeftShift) && moveZ == 1 && isGrounded)
{
walkSpeed = sprintSpeed;
}
if(moveZ == -1)
{
Debug.Log("Moving backwards");
}
Vector3 move = transform.right * moveX + transform.forward * moveZ;
controller.Move(move * walkSpeed * Time.deltaTime); ;
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}