You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.4 KiB
127 lines
3.4 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
private Rigidbody2D rb;
|
|
private SpriteRenderer SR;
|
|
private Transform Gr;
|
|
private Animator Ani;
|
|
private Collider2D C2D;
|
|
private Inputs Input;
|
|
private InputAction Walk;
|
|
private InputAction Jump;
|
|
private InputAction Dash;
|
|
private Vector2 Force;
|
|
private ParticleSystem Fart;
|
|
private float x;
|
|
public float drag;
|
|
public LayerMask GroundLayer;
|
|
public Vector2 dashvec;
|
|
public float Speed;
|
|
public float JumpPwr;
|
|
private void Awake()
|
|
{
|
|
Input = new Inputs();
|
|
rb = this.gameObject.GetComponent<Rigidbody2D>();
|
|
Gr = this.gameObject.transform.Find("GroundCheck").transform;
|
|
Ani = this.gameObject.GetComponent<Animator>();
|
|
SR = this.gameObject.GetComponent<SpriteRenderer>();
|
|
C2D = this.gameObject.GetComponent<Collider2D>();
|
|
Fart = this.gameObject.transform.Find("Fart").GetComponent<ParticleSystem>();
|
|
}
|
|
private void OnEnable()
|
|
{
|
|
Walk = Input.Movement.wasd;
|
|
Jump = Input.Movement.Jump;
|
|
Dash = Input.Movement.Dash;
|
|
Walk.Enable();
|
|
Jump.Enable();
|
|
Dash.Enable();
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
Walk.Disable();
|
|
Jump.Disable();
|
|
Dash.Disable();
|
|
}
|
|
private void Update()
|
|
{
|
|
Jump.performed += _ => jump();
|
|
Dash.performed += _ => dash();
|
|
Force = Walk.ReadValue<Vector2>();
|
|
if(Force.x < 0)
|
|
{
|
|
SR.flipX = true;
|
|
Fart.transform.localPosition = new Vector3(2.14f, -6.64f, 0);
|
|
Fart.transform.localRotation = Quaternion.Euler(new Vector3(30, 90, 0));
|
|
Ani.SetBool("Running", true);
|
|
C2D.offset = new Vector2(1.125f, -5.5f);
|
|
}
|
|
else if(Force.x > 0)
|
|
{
|
|
SR.flipX = false;
|
|
Fart.transform.localPosition = new Vector3(-2.14f, -6.64f, 0);
|
|
Fart.transform.localRotation = Quaternion.Euler(new Vector3(30, -90, 0));
|
|
Ani.SetBool("Running", true);
|
|
C2D.offset = new Vector2(-1.125f, -5.5f);
|
|
}
|
|
else
|
|
{
|
|
Ani.SetBool("Running", false);
|
|
}
|
|
if(rb.velocity.y < 0 && !IsGrounded())
|
|
{
|
|
Ani.SetBool("Falling", true);
|
|
}
|
|
else
|
|
{
|
|
Ani.SetBool("Falling", false);
|
|
}
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
rb.velocity = new Vector2(x + Force.x*Speed, rb.velocity.y);
|
|
if(x > -0.01 && x < 0.01 || IsGrounded())
|
|
{
|
|
x=0;
|
|
}
|
|
else
|
|
{
|
|
x = Mathf.Lerp(x, 0, Time.deltaTime * drag);
|
|
}
|
|
}
|
|
private bool IsGrounded()
|
|
{
|
|
return Physics2D.OverlapBox(Gr.position, new Vector2(6, 1), 0f, GroundLayer);
|
|
}
|
|
private void jump()
|
|
{
|
|
if(IsGrounded())
|
|
{
|
|
rb.velocity = new Vector2(rb.velocity.x, JumpPwr);
|
|
Ani.SetBool("Jump", true);
|
|
}
|
|
}
|
|
private void dash()
|
|
{
|
|
if(!IsGrounded())
|
|
{
|
|
float e = 0;
|
|
if(SR.flipX)
|
|
{
|
|
e = -1;
|
|
}
|
|
else
|
|
{
|
|
e = 1;
|
|
}
|
|
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + dashvec.y);
|
|
x += dashvec.x * e;
|
|
Fart.Play();
|
|
}
|
|
}
|
|
} |