Advertisement
valik140201

Untitled

Jan 9th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. public class PlayerMove : MonoBehaviour
  2. {
  3.     public float horSpeed = 10;
  4.     public float verticaImpulse;
  5.     static float speedX;
  6.  
  7.     public Transform child;
  8.  
  9.     bool isGrounded;
  10.     bool useShot;
  11.  
  12.     new private Rigidbody2D rigidbody;
  13.     private Animator animator;
  14.  
  15.     private Snowball snowball;
  16.  
  17.  
  18.  
  19.     private void Awake()
  20.     {
  21.         rigidbody = GetComponent<Rigidbody2D>();
  22.         animator = GetComponent<Animator>();
  23.         snowball = Resources.Load<Snowball>("Snowball");
  24.     }
  25.  
  26.     // Перемещенние и прыжок
  27.  
  28.     public void LeftButtonDown()
  29.     {
  30.         speedX = -horSpeed;
  31.         child.transform.rotation = Quaternion.Euler(0, 180, 0);
  32.         State = PlayerState.movehor;
  33.         useShot = false;
  34.     }
  35.  
  36.     public void RightButtonDown()
  37.     {
  38.         speedX = horSpeed;
  39.         child.transform.rotation = Quaternion.Euler(0, 0, 0);
  40.         State = PlayerState.movehor;
  41.         useShot = false;
  42.     }
  43.  
  44.     public void StopMove()
  45.     {
  46.         speedX = 0;
  47.         State = PlayerState.idle;
  48.     }
  49.  
  50.     public void OnClickJumb()
  51.     {
  52.         if(isGrounded)
  53.         rigidbody.AddForce(new Vector2(0, verticaImpulse * 100 * Time.deltaTime), ForceMode2D.Impulse);
  54.     }
  55.  
  56.     public void OnCLickShoot()
  57.     {
  58.         useShot = true;
  59.         Invoke("ShotSnowball", 0.417f);
  60.     }
  61.  
  62.     public void ShotSnowball()
  63.     {
  64.         Vector3 position = transform.position;
  65.         position.z = 10.0f;
  66.         position.y = -0.17f;
  67.         Snowball newSnowball = Instantiate(snowball, position, snowball.transform.rotation) as Snowball;
  68.         if (child.eulerAngles.y == 180)
  69.         {
  70.             newSnowball.Direction = newSnowball.transform.right * -1.0f;
  71.         }
  72.         if (child.eulerAngles.y == 0)
  73.         {
  74.             newSnowball.Direction = newSnowball.transform.right * 1.0f;
  75.         }
  76.     }
  77.  
  78.  
  79.     void FixedUpdate()
  80.     {
  81.         transform.Translate(speedX * Time.deltaTime, 0, 0);        
  82.     }
  83.  
  84.     void Update()
  85.     {
  86.         animator.SetBool("Shot", useShot);
  87.         useShot = false;
  88.     }
  89.  
  90.     // Проверка на соприкосновения с землей
  91.     private void OnCollisionEnter2D(Collision2D collision)
  92.     {
  93.         if (collision.gameObject.tag == "Ground")
  94.         {
  95.             isGrounded = true;
  96.         }
  97.     }
  98.  
  99.     private void OnCollisionExit2D(Collision2D collision)
  100.     {
  101.         if (collision.gameObject.tag == "Ground")
  102.         {
  103.             isGrounded = false;
  104.         }
  105.     }
  106.  
  107.     private PlayerState State
  108.     {
  109.         get { return (PlayerState)animator.GetInteger("State"); }
  110.         set { animator.SetInteger("State", (int)value); }
  111.     }
  112.  
  113. }
  114.  
  115. public enum PlayerState
  116. {
  117.     idle,
  118.     movehor
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement