Skip to content

Instantly share code, notes, and snippets.

@LottieVixen
Created March 9, 2021 22:08
Show Gist options
  • Save LottieVixen/d76f8f560e7c0200f985546f20ec5e96 to your computer and use it in GitHub Desktop.
Save LottieVixen/d76f8f560e7c0200f985546f20ec5e96 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
[SerializeField] private float GroundedWait = 60f;
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
// Update is called once per frame
void LateUpdate()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
sw.Start();
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
public void OnLanding()
{
if (sw.ElapsedMilliseconds > GroundedWait)
{
animator.SetBool("IsJumping", false);
sw.Reset();
}
}
private void FixedUpdate()
{
//move char
controller.Move(horizontalMove * Time.fixedDeltaTime,crouch,jump);
jump = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment