Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Mirror;
- using UnityEngine.UI;
- public class PlayerMovement : NetworkBehaviour
- {
- [SyncVar(hook = "OnChangeHealth")]public float currentHp;
- public float maxHp;
- public RectTransform HealthBar;
- public float speed;
- public float jumpForce;
- private float moveInput;
- private Rigidbody2D rb;
- private bool facingRight = true;
- private bool isGrounded;
- public Transform groundCheck;
- public float checkRadius;
- public LayerMask whatIsGround;
- private int extraJumps;
- public int extraJumpsValue;
- private float jumpTimeCounter;
- public float jumpTime;
- private bool isJumping;
- public SpriteRenderer playerSprite;
- //public GameObject cinematicCam;
- // Start is called before the first frame update
- void Start()
- {
- if (isLocalPlayer)
- {
- currentHp = maxHp;
- OnChangeHealth(maxHp);
- //cinematicCam.GetComponent<CinemachineShot>().
- extraJumps = extraJumpsValue;
- rb = GetComponent<Rigidbody2D>();
- }
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- if (isLocalPlayer)
- {
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
- moveInput = Input.GetAxisRaw("Horizontal");
- //Debug.Log(moveInput);
- rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
- if(facingRight == false && moveInput > 0)
- {
- Flip();
- }else if(facingRight == true && moveInput < 0)
- {
- Flip();
- }
- }
- }
- private void Update()
- {
- if (isLocalPlayer)
- {
- if (isGrounded == true)
- {
- extraJumps = extraJumpsValue;
- }
- if (Input.GetKeyDown(KeyCode.Space ) && extraJumps > 0)
- {
- isJumping = true;
- jumpTimeCounter = jumpTime;
- rb.velocity = Vector2.up * jumpForce;
- extraJumps--;
- }else if(Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
- {
- isJumping = true;
- jumpTimeCounter = jumpTime;
- rb.velocity = Vector2.up * jumpForce;
- }
- if (Input.GetKey(KeyCode.Space) && isJumping == true)
- {
- if(jumpTimeCounter > 0)
- {
- rb.velocity = Vector2.up * jumpForce;
- jumpTimeCounter -= Time.deltaTime;
- }
- else
- {
- isJumping = false;
- }
- }
- if (Input.GetKeyUp(KeyCode.Space))
- {
- isJumping = false;
- }
- }
- }
- void Flip()
- {
- if (isLocalPlayer)
- {
- facingRight = !facingRight;
- Vector3 Scaler = transform.localScale;
- Scaler.x *= -1;
- playerSprite.transform.localScale = Scaler;
- }
- }
- public void TakeDamage(float damage)
- {
- if (!isLocalPlayer)
- {
- return;
- }
- currentHp -= damage;
- //HealthBar.sizeDelta = new Vector2(currentHp, HealthBar.sizeDelta.y);
- }
- void OnChangeHealth(float health)
- {
- HealthBar.sizeDelta = new Vector2(currentHp, HealthBar.sizeDelta.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement