AlexRaynor

5.5 Player

Nov 12th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.    [SerializeField] private float speed = 2.5f;
  8.     public float Speed
  9.     {
  10.         get { return speed; }
  11.         set
  12.         {
  13.             if (value > 0.5)
  14.                 speed = value;
  15.         }
  16.     }
  17.  
  18.     [SerializeField] private float force;
  19.     public float Force
  20.     {
  21.         get { return force; }
  22.         set
  23.         {
  24.             if (value > 1.0)
  25.                 force = value;
  26.         }
  27.     }
  28.  
  29.  
  30.     [SerializeField] private Rigidbody2D rigidboby;
  31.  
  32.     public Rigidbody2D Rigidboby
  33.     {
  34.         get { return rigidboby; }
  35.         set
  36.         {
  37.                 rigidboby = value;
  38.         }
  39.     }
  40.  
  41.  
  42.     [SerializeField] private float minimalHeight;
  43.     public float MinimalHeight
  44.     {
  45.         get { return minimalHeight; }
  46.         set
  47.         {
  48.             if (value < 0.1)
  49.                 minimalHeight = value;
  50.         }
  51.     }
  52.  
  53.  
  54.     [SerializeField] private bool isCheatMode;
  55.     public bool IsCheatMode
  56.     {
  57.         get { return isCheatMode; }
  58.         set
  59.         {
  60.                 isCheatMode = value;
  61.         }
  62.     }
  63.  
  64.  
  65.     [SerializeField] private GroundDetection groundDetection;
  66.     public GroundDetection GroundDetection
  67.     {
  68.         get { return groundDetection; }
  69.         set
  70.         {
  71.             groundDetection = value;
  72.         }
  73.     }
  74.  
  75.  
  76.     [SerializeField] private Vector3 direction;
  77.     public Vector3 Direction
  78.     {
  79.         get { return direction; }
  80.         set
  81.         {
  82.             direction = value;
  83.         }
  84.     }
  85.  
  86.     [SerializeField] private Animator animator;
  87.     public Animator Animator
  88.     {
  89.         get { return animator; }
  90.         set
  91.         {
  92.             animator = value;
  93.         }
  94.     }
  95.  
  96.  
  97.  
  98.     [SerializeField] private SpriteRenderer spriteRenderer;
  99.     public SpriteRenderer SpriteRenderer
  100.     {
  101.         get { return spriteRenderer; }
  102.         set
  103.         {
  104.             spriteRenderer = value;
  105.         }
  106.     }
  107.  
  108.  
  109.  
  110.     [SerializeField] private bool isJumping;
  111.     public bool IsJumping
  112.     {
  113.         get { return isJumping; }
  114.         set
  115.         {
  116.             isJumping = value;
  117.         }
  118.     }
  119.  
  120.  
  121.     private void Update()
  122.     {
  123.         if (Input.GetKeyDown(KeyCode.Space) && groundDetection.isGrounded)
  124.         {
  125.             rigidboby.velocity = Vector3.zero; //
  126.  
  127.             rigidboby.AddForce(Vector2.up * force, ForceMode2D.Impulse);
  128.             animator.SetTrigger("StartJump");
  129.             isJumping = true;
  130.         }
  131.     }
  132.  
  133.  
  134.     void FixedUpdate()
  135.     {
  136.         animator.SetBool("isGrounded", groundDetection.isGrounded);
  137.         if (!isJumping && !groundDetection.isGrounded)
  138.         {
  139.             animator.SetTrigger("StartFall");
  140.         }
  141.         isJumping = isJumping && !groundDetection.isGrounded;
  142.         direction = Vector3.zero;
  143.         if (Input.GetKey(KeyCode.A))
  144.             direction = Vector3.left; // (-1, 0)
  145.         if (Input.GetKey(KeyCode.D))
  146.             direction = Vector3.right; // (1, 0)
  147.         direction *= speed;
  148.         direction.y = rigidboby.velocity.y;
  149.         rigidboby.velocity = direction;
  150.  
  151.  
  152.         if (direction.x > 0)
  153.             spriteRenderer.flipX = false;
  154.         if (direction.x < 0)
  155.             spriteRenderer.flipX = true;
  156.  
  157.         animator.SetFloat("Speed", Mathf.Abs(rigidboby.velocity.x));
  158.         CheckFall();
  159.     }
  160.  
  161.     void CheckFall()
  162.     {
  163.         if (transform.position.y < minimalHeight && isCheatMode)
  164.         {
  165.             rigidboby.velocity = new Vector2(0, 0);
  166.             transform.position = new Vector2(0, 0);
  167.         }
  168.         else if (transform.position.y < minimalHeight && !isCheatMode)
  169.             Destroy(gameObject);
  170.     }
  171.  
  172.     private void OnTriggerEnter2D(Collider2D col)
  173.     {
  174.         if (col.gameObject.CompareTag("Coin"))
  175.         {
  176.  
  177.             PlayerInventory.Instance.coinsCount++;
  178.             Debug.Log("количество монет = " + PlayerInventory.Instance.coinsCount);
  179.             Destroy(col.gameObject);
  180.         }
  181.     }
  182.  
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment