AlexRaynor

5.3 Player

Oct 28th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     public float speed = 2.5f;
  8.     public float force;
  9.     public Rigidbody2D rigidboby;
  10.     public float minimalHeight;
  11.     public bool isCheatMode;
  12.     public GroundDetection groundDetection;
  13.     private Vector3 direction;
  14.     public Animator animator;
  15.     public SpriteRenderer spriteRenderer;
  16.     private bool isJumping;
  17.  
  18.  
  19.  
  20.     private void Update()
  21.     {
  22.         if (Input.GetKeyDown(KeyCode.Space) && groundDetection.isGrounded)
  23.         {
  24.             rigidboby.velocity = Vector3.zero; //
  25.  
  26.             rigidboby.AddForce(Vector2.up * force, ForceMode2D.Impulse);
  27.             animator.SetTrigger("StartJump");
  28.             isJumping = true;
  29.         }
  30.     }
  31.  
  32.  
  33.     void FixedUpdate()
  34.     {
  35.         animator.SetBool("isGrounded", groundDetection.isGrounded);
  36.         if (!isJumping && !groundDetection.isGrounded)
  37.         {
  38.             animator.SetTrigger("StartFall");
  39.         }
  40.         isJumping = isJumping && !groundDetection.isGrounded;
  41.         direction = Vector3.zero;
  42.         if (Input.GetKey(KeyCode.A))
  43.             direction = Vector3.left; // (-1, 0)
  44.         if (Input.GetKey(KeyCode.D))
  45.             direction = Vector3.right; // (1, 0)
  46.         direction *= speed;
  47.         direction.y = rigidboby.velocity.y;
  48.         rigidboby.velocity = direction;
  49.  
  50.  
  51.         if (direction.x > 0)
  52.             spriteRenderer.flipX = false;
  53.         if (direction.x < 0)
  54.             spriteRenderer.flipX = true;
  55.  
  56.         animator.SetFloat("Speed", Mathf.Abs(rigidboby.velocity.x));
  57.         CheckFall();
  58.     }
  59.  
  60.     void CheckFall()
  61.     {
  62.         if (transform.position.y < minimalHeight && isCheatMode)
  63.         {
  64.             rigidboby.velocity = new Vector2(0, 0);
  65.             transform.position = new Vector2(0, 0);
  66.         }
  67.         else if (transform.position.y < minimalHeight && !isCheatMode)
  68.             Destroy(gameObject);
  69.     }
  70.  
  71.     private void OnTriggerEnter2D(Collider2D col)
  72.     {
  73.         if (col.gameObject.CompareTag("Coin"))
  74.         {
  75.  
  76.             PlayerInventory.Instance.coinsCount++;
  77.             Debug.Log("количество монет = " + PlayerInventory.Instance.coinsCount);
  78.             Destroy(col.gameObject);
  79.         }
  80.     }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment