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.
RatAttack2D/Assets/Scripts/PlayerMovement.cs

143 lines
3.5 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody2D rb;
private Transform Gr;
private Animator Ani;
private Inputs Input;
private InputAction Walk;
private InputAction Jump;
private InputAction Dash;
private InputAction Aim;
private ParticleSystem Fart;
private float x;
private int usedash;
private bool jumpin;
private bool dashin;
2 years ago
public LayerMask g;
public Vector2 dashvec;
2 years ago
public float drag;
public int dashes;
public float Speed;
public float JumpPwr;
[System.NonSerialized] public Vector2 Overlap;
private void Awake()
{
Overlap = new Vector2(6, 1);
Input = new Inputs();
rb = this.gameObject.GetComponent<Rigidbody2D>();
2 years ago
Gr = this.transform.Find("GroundCheck").transform;
Ani = this.gameObject.GetComponent<Animator>();
2 years ago
Fart = this.transform.Find("Fart").GetComponent<ParticleSystem>();
}
private void OnEnable()
{
Walk = Input.Movement.wasd;
Jump = Input.Movement.Jump;
Dash = Input.Movement.Dash;
Aim = Input.Movement.Aim;
Walk.Enable();
Jump.Enable();
Dash.Enable();
Aim.Enable();
}
private void OnDisable()
{
Walk.Disable();
Jump.Disable();
Dash.Disable();
Aim.Disable();
}
private void Update()
{
int ani = 0;
2 years ago
Vector2 Force = Walk.ReadValue<Vector2>();
rb.velocity = new Vector2(x + Force.x * Speed, rb.velocity.y);
if (Force.x < 0)
{
2 years ago
if (x > 0)
{
x = -x;
}
ani = 1;
2 years ago
rb.transform.rotation = new Quaternion(0, 180, 0, 0);
}
2 years ago
else if (Force.x > 0)
{
2 years ago
if (x < 0)
{
x = -x;
}
ani = 1;
2 years ago
rb.transform.rotation = new Quaternion(0, 0, 0, 0);
}
2 years ago
if (rb.velocity.y < 0 && !IsGrounded())
{
ani = 4;
jumpin = false;
}
2 years ago
if (IsGrounded())
{
2 years ago
if (x > -0.01 && x < 0.01)
{
x = 0;
}
else
{
x = Mathf.Lerp(x, 0, Time.deltaTime * drag);
}
usedash = dashes;
dashin = false;
}
if (Jump.WasPressedThisFrame() || jumpin)
{
jump();
ani = 2;
}
if (Dash.WasPressedThisFrame() || dashin)
{
dash();
if (!IsGrounded())
{
ani = 5;
}
}
if (Aim.IsPressed())
{
ani = 6;
}
Ani.SetInteger("Anim", ani);
}
private bool IsGrounded()
{
return Physics2D.OverlapBox(Gr.position, Overlap, 0f, g);
}
private void jump()
{
2 years ago
if (IsGrounded())
{
jumpin = true;
rb.velocity = new Vector2(rb.velocity.x, JumpPwr);
}
}
private void dash()
{
2 years ago
if (!IsGrounded() && !Fart.isPlaying && usedash != 0)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y + dashvec.y);
2 years ago
x += dashvec.x;
if (rb.transform.eulerAngles.y == 180)
{
x = -x;
}
Fart.Play();
usedash--;
dashin = true;
}
}
}