Animation
//PlayerMovement
public float PlayerSpeed = 1.9f;
public float PlayerSprint = 3f;
//PlayerCamera
public Transform PlayerCamera;
//Animator & Gravity
public CharacterController cC;
public float gravity = -9.81f;
public Animator animator;
//Player Jumping & Valocity
public float jumpRange = 1f;
Vector3 velocity;
public float turnCalmTime = 0.1f;
float turnCalmValocity;
public Transform SurfaceCheck;
bool OnSurface;
public float surfaceDistance = 0.4f;
public LayerMask surfaceMusk;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
OnSurface = Physics.CheckSphere(SurfaceCheck.position, surfaceDistance, surfaceMusk);
if(OnSurface && velocity.y < 0)
{
velocity.y = -2f;
}
//gravity
velocity.y += gravity * Time.deltaTime;
cC.Move(velocity * Time.deltaTime);
PlayerMove();
jump();
Sprint();
}
void PlayerMove()
{
float Horizontal_Move = Input.GetAxisRaw("Horizontal");
float vertical_Move = Input.GetAxisRaw("Vertical");
Vector3 direction_Move = new Vector3(Horizontal_Move, 0f, vertical_Move).normalized;
if(direction_Move.magnitude >= 0.1f)
{
animator.SetBool("Walk", true);
animator.SetBool("Running", false);
animator.SetBool("Idle", false);
animator.SetTrigger("Jump");
animator.SetBool("AimWalk", false);
animator.SetBool("IdleAim", false);
float target_Angle = Mathf.Atan2(direction_Move.x, direction_Move.z) * Mathf.Rad2Deg + PlayerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target_Angle,ref turnCalmValocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 direction_Cam = Quaternion.Euler(0f, target_Angle, 0f) * Vector3.forward;
cC.Move(direction_Cam.normalized * PlayerSpeed * Time.deltaTime);
}
else
{
animator.SetBool("Idle", true);
animator.SetTrigger("Jump");
animator.SetBool("Walk", false);
animator.SetBool("Running", false);
animator.SetBool("AimWalk", false);
}
}
void jump()
{
if(Input.GetButtonDown("Jump")&& OnSurface)
{
animator.SetBool("Walk", false);
animator.SetBool("Running", false);
animator.SetTrigger("Jump");
velocity.y = Mathf.Sqrt(jumpRange * -2 * gravity);
}
else
{
animator.ResetTrigger("Jump");
}
}
void Sprint()
{
if (Input.GetButton("Sprint")&&Input.GetKey(KeyCode.W)||Input.GetKey(KeyCode.UpArrow)&&OnSurface)
{
animator.SetBool("Running", true);
animator.SetBool("Idle", false);
animator.SetBool("Walk", false);
animator.SetBool("IdleAim",false);
float Horizontal_Move = Input.GetAxisRaw("Horizontal");
float vertical_Move = Input.GetAxisRaw("Vertical");
Vector3 direction_Move = new Vector3(Horizontal_Move, 0f, vertical_Move).normalized;
if (direction_Move.magnitude >= 0.1f)
{
float target_Angle = Mathf.Atan2(direction_Move.x, direction_Move.z) * Mathf.Rad2Deg + PlayerCamera.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, target_Angle, ref turnCalmValocity, turnCalmTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 direction_Cam = Quaternion.Euler(0f, target_Angle, 0f) * Vector3.forward;
cC.Move(direction_Cam.normalized * PlayerSprint * Time.deltaTime);
}
else
{
animator.SetBool("Idle", false);
animator.SetBool("Walk", false);
}
}
}