Advertisement
Guest User

Movement script

a guest
Sep 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     [Range(0, 10)] public float _speed = 5f;
  8.     private Transform _playerT;
  9.     private Rigidbody2D _playerRb;
  10.     private Collider2D _playerCol;
  11.     private Animator _playerAnim;
  12.     private SpriteRenderer _playerSprite;
  13.     private IsGrounded _igScript; //Скрипт, который поможет нам понять, стоим ли мы НОГАМИ на платформе, или нет
  14.  
  15.     // Start is called before the first frame update
  16.     void Start()
  17.     {
  18.         _playerT = GetComponent<Transform>();
  19.         _playerRb = GetComponent<Rigidbody2D>();
  20.         _playerCol = GetComponent<Collider2D>();
  21.         _playerAnim = GetComponent<Animator>();
  22.         _playerSprite = GetComponent<SpriteRenderer>();
  23.         _igScript = GetComponentInChildren<IsGrounded>();
  24.        
  25.         /*
  26.           Т.К сам по себе скрипт IsGrounded- это новый тип компонента, он может быть прикреплен к любому объекту,
  27.           убеждаемся, что мы имеем дело со скриптом именно НИЖНЕГО КОЛЛАЙДЕРА
  28.         */
  29.     }
  30.  
  31.     private void Update()
  32.     {
  33.         if (Input.GetKey(KeyCode.Space) && _igScript._isGrounded)
  34.             _playerRb.velocity=(new Vector2(0, 10));
  35.  
  36.        
  37.     }
  38.  
  39.     // Update is called once per frame
  40.     void FixedUpdate()
  41.     {
  42.         MoveX();
  43.     }
  44.  
  45.     public void MoveX()
  46.     {
  47.         ector2 direction = new Vector2(Input.GetAxis("Horizontal"), 1);
  48.  
  49.         if (direction.x != 0)
  50.             _playerAnim.SetInteger("AnimId", 1);
  51.         else
  52.             _playerAnim.SetInteger("AnimId", 0);
  53.  
  54.  
  55.         _playerSprite.flipX = (direction.x < 0);
  56.  
  57.  
  58.         _playerRb.position = new Vector2(_playerT.position.x + direction.x * Time.deltaTime * _speed, _playerT.position.y);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement