Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7.    //Zmienne do fizyki, animacji, warunku czy stoi na ziemi
  8.     private Rigidbody2D m_Rigidbody;
  9.     private Animator m_PlayerAnimator;
  10.     private bool m_Grounded = true;
  11.     private bool m_Running = false;
  12.  
  13.     //Predkosc chodzenia i sila skoku
  14.     public float m_Speed = 6;
  15.     public float m_JumpForce = 200;
  16.  
  17.  
  18.  
  19.     void Start ()
  20.     {
  21.         m_Rigidbody = GetComponent<Rigidbody2D>();
  22.         m_PlayerAnimator = GetComponent<Animator>();
  23.     }
  24.  
  25.     void Update ()
  26.     {
  27.        
  28.     }
  29.  
  30.     private void FixedUpdate()
  31.     {
  32.         if (Input.GetButton("Horizontal"))
  33.         {
  34.             //Jezeli biegnie (m_Running = true)
  35.             m_Running = true;
  36.             m_Rigidbody.velocity = new Vector2(Input.GetAxis("Horizontal") * m_Speed, m_Rigidbody.velocity.y);
  37.             m_PlayerAnimator.SetBool("run", true);
  38.  
  39.             //Jezeli sie porusza to
  40.             if (m_Rigidbody.velocity != Vector2.zero)
  41.             {
  42.                 //Jezeli predkosc jest ujemna (biegnie w lewo) obroc go w lewo
  43.                 if (m_Rigidbody.velocity.x < 0)
  44.                 {
  45.                     transform.right = Vector2.left;
  46.                 }
  47.                 //Jezeli predkosc jest dodatnia (biegnie w prawo) obrog go w prawo
  48.                 else
  49.                 {
  50.                     transform.right = Vector2.right;
  51.                 }
  52.             }
  53.  
  54.         }
  55.         else
  56.         {
  57.             //Jezeli nie  biega (m_Running = false)
  58.             m_Running = false;
  59.             m_Rigidbody.velocity = Vector2.zero;
  60.             m_PlayerAnimator.SetBool("run", false);
  61.         }
  62.  
  63.  
  64.         //Jezeli klikniesz skacz i stoi na ziemi (m_Grounded == true) to skocz
  65.         if (Input.GetButton("Jump") && m_Grounded)
  66.         {
  67.             m_Rigidbody.AddForce(new Vector2(0, Input.GetAxis("Jump") * m_JumpForce));
  68.             m_PlayerAnimator.SetBool("jump", true);
  69.         }
  70.         else
  71.         {
  72.             m_PlayerAnimator.SetBool("jump", false);
  73.         }
  74.     }
  75.  
  76.     void OnTriggerEnter2D(Collider2D other)
  77.     {
  78.         //Jezeli trigger przy nogach dotyka ziemi (m_Grounded = true)
  79.         m_Grounded = true;
  80.         //Jezeli nie biegnie to
  81.         if(!m_Running)
  82.         {
  83.             //Do animacji stania wyslac prawde
  84.             m_PlayerAnimator.SetBool("stay", true);
  85.             //Do animacji skoku wyslac falsz
  86.             m_PlayerAnimator.SetBool("jump", false);
  87.         }
  88.         else
  89.         {
  90.             //Inaczej (jezeli biegnie) wyslac do biegu prawde a do skoku falsz
  91.             m_PlayerAnimator.SetBool("run", true);
  92.             m_PlayerAnimator.SetBool("jump", false);
  93.         }
  94.     }
  95.  
  96.     private void OnTriggerExit2D(Collider2D other)
  97.     {
  98.         //Jezeli nie dotyka ziemi (m_Grounded = false)
  99.         m_Grounded = false;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement