Advertisement
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
- {
- [Range(0, 10)] public float _speed = 5f;
- private Transform _playerT;
- private Rigidbody2D _playerRb;
- private Collider2D _playerCol;
- private Animator _playerAnim;
- private SpriteRenderer _playerSprite;
- private IsGrounded _igScript; //Скрипт, который поможет нам понять, стоим ли мы НОГАМИ на платформе, или нет
- // Start is called before the first frame update
- void Start()
- {
- _playerT = GetComponent<Transform>();
- _playerRb = GetComponent<Rigidbody2D>();
- _playerCol = GetComponent<Collider2D>();
- _playerAnim = GetComponent<Animator>();
- _playerSprite = GetComponent<SpriteRenderer>();
- _igScript = GetComponentInChildren<IsGrounded>();
- /*
- Т.К сам по себе скрипт IsGrounded- это новый тип компонента, он может быть прикреплен к любому объекту,
- убеждаемся, что мы имеем дело со скриптом именно НИЖНЕГО КОЛЛАЙДЕРА
- */
- }
- private void Update()
- {
- if (Input.GetKey(KeyCode.Space) && _igScript._isGrounded)
- _playerRb.velocity=(new Vector2(0, 10));
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- MoveX();
- }
- public void MoveX()
- {
- ector2 direction = new Vector2(Input.GetAxis("Horizontal"), 1);
- if (direction.x != 0)
- _playerAnim.SetInteger("AnimId", 1);
- else
- _playerAnim.SetInteger("AnimId", 0);
- _playerSprite.flipX = (direction.x < 0);
- _playerRb.position = new Vector2(_playerT.position.x + direction.x * Time.deltaTime * _speed, _playerT.position.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement