Facebook
From Loi, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 268
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using Unity.Mathematics;
  5. using Unity.VisualScripting;
  6. using UnityEditor;
  7. using UnityEditor.Callbacks;
  8. using UnityEditor.U2D;
  9. using UnityEngine;
  10. using UnityEngine.U2D.IK;
  11. using UnityEngine.UIElements;
  12.  
  13.  
  14. public class NewBehaviourScript : MonoBehaviour
  15. {
  16.  
  17.     [Header("Move")]
  18.     public float playerInput;
  19.     [SerializeField]private Rigidbody2D playerRb;
  20.     [SerializeField]private float mSpeed;
  21.     public bool facingRight=false;
  22.  
  23.  
  24.     [Header("Jump")]
  25.     [SerializeField]private float height;
  26.     [SerializeField]private float gravityF;
  27.     [SerializeField]private float gravityH;
  28.     [SerializeField]private float JumpStarttime;
  29.     [SerializeField]private float coyateTime;
  30.     [SerializeField]private float coyateCounter;
  31.     private float jumpTime;
  32.     private bool isJump;
  33.  
  34.     [Header("Dash")]
  35.     [SerializeField]private float dashPower=10f;
  36.     [SerializeField]private float dashCooldown;
  37.     [SerializeField]private float dashCooldownCounter;
  38.    
  39.  
  40.     [Header("Gr")]
  41.     [SerializeField]LayerMask Ground;
  42.     [SerializeField]Transform feetPosition;
  43.     [SerializeField]float radius;
  44.     private bool onGr;
  45.  
  46.  
  47.  
  48.     void Start()
  49.     {
  50.         dashCooldownCounter=dashCooldown;
  51.         playerRb.drag=0;
  52.     }
  53.     void Update()
  54.     {
  55.          Debug.Log(playerRb.velocity);
  56.         //JUmp
  57.         if(Input.GetKeyDown(KeyCode.Space)&&coyateCounter;>0f)
  58.         {
  59.             playerRb.AddForce(new Vector2(transform.position.x,height),ForceMode2D.Impulse);
  60.             isJump=true;
  61.             jumpTime=JumpStarttime;//đặt thời gian để chạy ngược quy định tg giữ nút cách
  62.         }
  63.  
  64.        
  65.         if(Input.GetKeyUp( KeyCode.Space))
  66.         {
  67.             isJump=false;
  68.         }
  69.  
  70.         if(onGr==true)
  71.         {
  72.             coyateCounter=coyateTime;
  73.         }
  74.         else
  75.         {
  76.             coyateCounter-=Time.deltaTime;
  77.         }
  78.         onGr=Physics2D.OverlapCircle(feetPosition.position,radius,Ground);
  79.         //flip
  80.         if(playerInput==1&& facingRight==false)
  81.         {
  82.             Flip();
  83.         }
  84.         if(playerInput==-1&& facingRight==true)
  85.         {
  86.             Flip();
  87.         }
  88.         Dash();
  89.  
  90.        
  91.     }
  92.    
  93.     void smoothJu()
  94.     {
  95.         if(playerRb.velocity.y<0)
  96.         {
  97.             playerRb.velocity+=Vector2.up * Physics2D.gravity.y * (gravityF-1) * Time.deltaTime;
  98.             //tốc độ rơi tăng dần theo tg
  99.         }
  100.         else if(playerRb.velocity.y>0 && isJump)
  101.         {
  102.             playerRb.AddForce(Vector2.down * Physics2D.gravity.y * (gravityH-1) * Time.deltaTime,ForceMode2D.Impulse);
  103.         }
  104.     }
  105.     private void FixedUpdate()
  106.     {
  107.      
  108.         smoothJu();
  109.         playerInput=Input.GetAxisRaw("Horizontal");
  110.         playerRb.velocity=new Vector2(mSpeed*playerInput,playerRb.velocity.y);
  111.  
  112.     }
  113.    
  114.     void Flip()
  115.     {
  116.         {
  117.             Vector2 currentScale = gameObject.transform.localScale;
  118.             currentScale.x *= -1;
  119.             gameObject.transform.localScale = currentScale;
  120.             facingRight = !facingRight;
  121.          }
  122.     }
  123.     void HoldJump()
  124.     {
  125.          if(Input.GetKey(KeyCode.Space)&&isJump;==true&&coyateCounter;>0f)
  126.         {
  127.             if(jumpTime>0)
  128.             {
  129.              playerRb.velocity=Vector2.up*height;
  130.              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
  131.             }
  132.             else
  133.             {
  134.                 isJump=false;
  135.             }
  136.         }
  137.     }
  138.     void Dash()
  139.     {
  140.            if(Input.GetKeyDown(KeyCode.Q))
  141.         {
  142.             playerRb.velocity=new Vector2(dashPower,playerRb.velocity.y);
  143.         }
  144.     }
  145. }    
  146.  
  147.