Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- [SerializeField] private float speed = 2.5f;
- public float Speed
- {
- get { return speed; }
- set
- {
- if (value > 0.5)
- speed = value;
- }
- }
- [SerializeField] private float force;
- public float Force
- {
- get { return force; }
- set
- {
- if (value > 1.0)
- force = value;
- }
- }
- [SerializeField] private Rigidbody2D rigidboby;
- public Rigidbody2D Rigidboby
- {
- get { return rigidboby; }
- set
- {
- rigidboby = value;
- }
- }
- [SerializeField] private float minimalHeight;
- public float MinimalHeight
- {
- get { return minimalHeight; }
- set
- {
- if (value < 0.1)
- minimalHeight = value;
- }
- }
- [SerializeField] private bool isCheatMode;
- public bool IsCheatMode
- {
- get { return isCheatMode; }
- set
- {
- isCheatMode = value;
- }
- }
- [SerializeField] private GroundDetection groundDetection;
- public GroundDetection GroundDetection
- {
- get { return groundDetection; }
- set
- {
- groundDetection = value;
- }
- }
- [SerializeField] private Vector3 direction;
- public Vector3 Direction
- {
- get { return direction; }
- set
- {
- direction = value;
- }
- }
- [SerializeField] private Animator animator;
- public Animator Animator
- {
- get { return animator; }
- set
- {
- animator = value;
- }
- }
- [SerializeField] private SpriteRenderer spriteRenderer;
- public SpriteRenderer SpriteRenderer
- {
- get { return spriteRenderer; }
- set
- {
- spriteRenderer = value;
- }
- }
- [SerializeField] private bool isJumping;
- public bool IsJumping
- {
- get { return isJumping; }
- set
- {
- isJumping = value;
- }
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space) && groundDetection.isGrounded)
- {
- rigidboby.velocity = Vector3.zero; //
- rigidboby.AddForce(Vector2.up * force, ForceMode2D.Impulse);
- animator.SetTrigger("StartJump");
- isJumping = true;
- }
- }
- void FixedUpdate()
- {
- animator.SetBool("isGrounded", groundDetection.isGrounded);
- if (!isJumping && !groundDetection.isGrounded)
- {
- animator.SetTrigger("StartFall");
- }
- isJumping = isJumping && !groundDetection.isGrounded;
- direction = Vector3.zero;
- if (Input.GetKey(KeyCode.A))
- direction = Vector3.left; // (-1, 0)
- if (Input.GetKey(KeyCode.D))
- direction = Vector3.right; // (1, 0)
- direction *= speed;
- direction.y = rigidboby.velocity.y;
- rigidboby.velocity = direction;
- if (direction.x > 0)
- spriteRenderer.flipX = false;
- if (direction.x < 0)
- spriteRenderer.flipX = true;
- animator.SetFloat("Speed", Mathf.Abs(rigidboby.velocity.x));
- CheckFall();
- }
- void CheckFall()
- {
- if (transform.position.y < minimalHeight && isCheatMode)
- {
- rigidboby.velocity = new Vector2(0, 0);
- transform.position = new Vector2(0, 0);
- }
- else if (transform.position.y < minimalHeight && !isCheatMode)
- Destroy(gameObject);
- }
- private void OnTriggerEnter2D(Collider2D col)
- {
- if (col.gameObject.CompareTag("Coin"))
- {
- PlayerInventory.Instance.coinsCount++;
- Debug.Log("количество монет = " + PlayerInventory.Instance.coinsCount);
- Destroy(col.gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment