Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.  
  7.     public float speed = 5;
  8.     public float jumpForce = 4;
  9.  
  10.     public bool canJump = true;
  11.  
  12.     private Animator anim;
  13.     private Rigidbody2D rb2d;
  14.  
  15.  
  16.  
  17.     // Awake wykonuje sie jako pierwsze, sluzy do inicjalizacji
  18.     void Awake()
  19.     {
  20.         anim = GetComponent<Animator>();     // przypisuje zmiennej "anim" komponent Animator
  21.         rb2d = GetComponent<Rigidbody2D>();  // przypisuje zmiennej "rb2d" komponent Rigidbody2D
  22.     }
  23.  
  24.     // Update wykonuje sie tyle razy na sekunde ile mamy klatek w grze
  25.     void Update()
  26.     {
  27.         if (Input.GetKeyDown(KeyCode.Space) && canJump == true)  // Jezeli wcisnieto spacje i zmienna canJump == true
  28.         {
  29.             canJump = false; // ustaw canJump na false
  30.             rb2d.velocity = new Vector2(0, jumpForce); // odwoluje sie do komponentu Rigidbody2D by wykonal skok
  31.         }
  32.  
  33.     }
  34.  
  35.     void OnCollisionEnter2D(Collision2D coll) // Funkcja przygotowana przez unity do sprawdzania kolizji z komponentem rigidbody2d i colliderow 2d
  36.     {
  37.         canJump = true;
  38.     }
  39.  
  40.  
  41.     // FixedUpdate jest uzywany do obliczania fizyki/kolizji w czasie rzeczywistym, prawdopodobnie jest bardziej zasobozerny
  42.     void FixedUpdate()
  43.     {
  44.         float horizontalMove = Input.GetAxisRaw("Horizontal");
  45.  
  46.         rb2d.AddForce((Vector2.right * speed) * horizontalMove);
  47.  
  48.         if (rb2d.velocity.x > speed)
  49.         {
  50.             rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
  51.         }
  52.  
  53.  
  54.         if (rb2d.velocity.x < -speed)
  55.         {
  56.             rb2d.velocity = new Vector2(-speed, rb2d.velocity.y);
  57.         }
  58.  
  59.  
  60.         anim.SetBool("Grounded", canJump);
  61.         anim.SetFloat("Speed", horizontalMove);
  62.  
  63.         //anim.SetFloat("HorizontalMove", Mathf.Abs(horizontalMove));
  64.  
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement