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
- {
- public float speed = 2.5f;
- public float force;
- public Rigidbody2D rigidboby;
- public float minimalHeight;
- public bool isCheatMode;
- public GroundDetection groundDetection;
- private Vector3 direction;
- public Animator animator;
- public SpriteRenderer spriteRenderer;
- private bool isJumping;
- 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