- using System.Collections;
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- using Unity.Mathematics;
- using Unity.VisualScripting;
- using UnityEditor;
- using UnityEditor.Callbacks;
- using UnityEditor.U2D;
- using UnityEngine;
- using UnityEngine.U2D.IK;
- using UnityEngine.UIElements;
- public class NewBehaviourScript : MonoBehaviour
- {
- [Header("Move")]
- public float playerInput;
- [SerializeField]private Rigidbody2D playerRb;
- [SerializeField]private float mSpeed;
- public bool facingRight=false;
- [Header("Jump")]
- [SerializeField]private float height;
- [SerializeField]private float gravityF;
- [SerializeField]private float gravityH;
- [SerializeField]private float JumpStarttime;
- [SerializeField]private float coyateTime;
- [SerializeField]private float coyateCounter;
- private float jumpTime;
- private bool isJump;
- [Header("Dash")]
- [SerializeField]private float dashPower=10f;
- [SerializeField]private float dashCooldown;
- [SerializeField]private float dashCooldownCounter;
- [Header("Gr")]
- [SerializeField]LayerMask Ground;
- [SerializeField]Transform feetPosition;
- [SerializeField]float radius;
- private bool onGr;
- void Start()
- {
- dashCooldownCounter=dashCooldown;
- playerRb.drag=0;
- }
- void Update()
- {
- Debug.Log(playerRb.velocity);
- //JUmp
- if(Input.GetKeyDown(KeyCode.Space)&&coyateCounter;>0f)
- {
- playerRb.AddForce(new Vector2(transform.position.x,height),ForceMode2D.Impulse);
- isJump=true;
- jumpTime=JumpStarttime;//đặt thời gian để chạy ngược quy định tg giữ nút cách
- }
- if(Input.GetKeyUp( KeyCode.Space))
- {
- isJump=false;
- }
- if(onGr==true)
- {
- coyateCounter=coyateTime;
- }
- else
- {
- coyateCounter-=Time.deltaTime;
- }
- onGr=Physics2D.OverlapCircle(feetPosition.position,radius,Ground);
- //flip
- if(playerInput==1&& facingRight==false)
- {
- Flip();
- }
- if(playerInput==-1&& facingRight==true)
- {
- Flip();
- }
- Dash();
- }
- void smoothJu()
- {
- if(playerRb.velocity.y<0)
- {
- playerRb.velocity+=Vector2.up * Physics2D.gravity.y * (gravityF-1) * Time.deltaTime;
- //tốc độ rơi tăng dần theo tg
- }
- else if(playerRb.velocity.y>0 && isJump)
- {
- playerRb.AddForce(Vector2.down * Physics2D.gravity.y * (gravityH-1) * Time.deltaTime,ForceMode2D.Impulse);
- }
- }
- private void FixedUpdate()
- {
- smoothJu();
- playerInput=Input.GetAxisRaw("Horizontal");
- playerRb.velocity=new Vector2(mSpeed*playerInput,playerRb.velocity.y);
- }
- void Flip()
- {
- {
- Vector2 currentScale = gameObject.transform.localScale;
- currentScale.x *= -1;
- gameObject.transform.localScale = currentScale;
- facingRight = !facingRight;
- }
- }
- void HoldJump()
- {
- if(Input.GetKey(KeyCode.Space)&&isJump;==true&&coyateCounter;>0f)
- {
- if(jumpTime>0)
- {
- playerRb.velocity=Vector2.up*height;
- jumpTime-=Time.deltaTime;//nếu còn trong tg nhảy cao thì tăng lực nhảy và giảm dần tg nhảy = cooldown tg nhảy cao
- }
- else
- {
- isJump=false;
- }
- }
- }
- void Dash()
- {
- if(Input.GetKeyDown(KeyCode.Q))
- {
- playerRb.velocity=new Vector2(dashPower,playerRb.velocity.y);
- }
- }
- }