Advertisement
Guest User

Jump

a guest
Jul 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5. using UnityEngine.UI;
  6.  
  7. public class PlayerMovement : NetworkBehaviour
  8. {
  9.     [SyncVar(hook = "OnChangeHealth")]public float currentHp;
  10.     public float maxHp;
  11.     public RectTransform HealthBar;
  12.  
  13.  
  14.  
  15.  
  16.     public float speed;
  17.     public float jumpForce;
  18.     private float moveInput;
  19.  
  20.     private Rigidbody2D rb;
  21.  
  22.     private bool facingRight = true;
  23.  
  24.  
  25.     private bool isGrounded;
  26.     public Transform groundCheck;
  27.     public float checkRadius;
  28.     public LayerMask whatIsGround;
  29.  
  30.     private int extraJumps;
  31.     public int extraJumpsValue;
  32.  
  33.  
  34.     private float jumpTimeCounter;
  35.     public float jumpTime;
  36.  
  37.     private bool isJumping;
  38.  
  39.     public SpriteRenderer playerSprite;
  40.  
  41.  
  42.     //public GameObject cinematicCam;
  43.  
  44.     // Start is called before the first frame update
  45.     void Start()
  46.     {
  47.         if (isLocalPlayer)
  48.         {
  49.             currentHp = maxHp;
  50.             OnChangeHealth(maxHp);
  51.             //cinematicCam.GetComponent<CinemachineShot>().
  52.             extraJumps = extraJumpsValue;
  53.             rb = GetComponent<Rigidbody2D>();
  54.         }
  55.        
  56.     }
  57.  
  58.     // Update is called once per frame
  59.     void FixedUpdate()
  60.     {
  61.         if (isLocalPlayer)
  62.         {
  63.             isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
  64.  
  65.             moveInput = Input.GetAxisRaw("Horizontal");
  66.             //Debug.Log(moveInput);
  67.             rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
  68.  
  69.             if(facingRight == false && moveInput > 0)
  70.             {
  71.                 Flip();
  72.             }else if(facingRight == true && moveInput < 0)
  73.             {
  74.                 Flip();
  75.             }
  76.         }
  77.     }
  78.  
  79.     private void Update()
  80.     {
  81.         if (isLocalPlayer)
  82.         {
  83.             if (isGrounded == true)
  84.             {
  85.                 extraJumps = extraJumpsValue;
  86.             }
  87.  
  88.             if (Input.GetKeyDown(KeyCode.Space ) && extraJumps > 0)
  89.             {
  90.                 isJumping = true;
  91.                 jumpTimeCounter = jumpTime;
  92.                 rb.velocity = Vector2.up * jumpForce;
  93.                
  94.                 extraJumps--;
  95.             }else if(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
  96.             {
  97.                 isJumping = true;
  98.                 jumpTimeCounter = jumpTime;
  99.                 rb.velocity = Vector2.up * jumpForce;
  100.             }
  101.  
  102.             if (Input.GetKey(KeyCode.Space) && isJumping == true)
  103.             {
  104.                 if(jumpTimeCounter > 0)
  105.                 {
  106.                     rb.velocity = Vector2.up * jumpForce;
  107.                     jumpTimeCounter -= Time.deltaTime;
  108.                 }
  109.                 else
  110.                 {
  111.                     isJumping = false;
  112.                 }
  113.             }
  114.  
  115.             if (Input.GetKeyUp(KeyCode.Space))
  116.             {
  117.                 isJumping = false;
  118.             }
  119.  
  120.            
  121.         }
  122.     }
  123.  
  124.     void Flip()
  125.     {
  126.         if (isLocalPlayer)
  127.         {
  128.             facingRight = !facingRight;
  129.             Vector3 Scaler = transform.localScale;
  130.             Scaler.x *= -1;
  131.             playerSprite.transform.localScale = Scaler;
  132.         }
  133.     }
  134.  
  135.     public void TakeDamage(float damage)
  136.     {
  137.         if (!isLocalPlayer)
  138.         {
  139.             return;
  140.         }
  141.  
  142.         currentHp -= damage;
  143.         //HealthBar.sizeDelta = new Vector2(currentHp, HealthBar.sizeDelta.y);
  144.     }
  145.  
  146.     void OnChangeHealth(float health)
  147.     {
  148.         HealthBar.sizeDelta = new Vector2(currentHp, HealthBar.sizeDelta.y);
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement