Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Axe : MonoBehaviour {
  6.  
  7.     public Rigidbody rb;
  8.     private Vector3 startPosition;
  9.     private Quaternion startRotation;
  10.  
  11.     public float force = 5;  //mnoznik sily
  12.     public float torque;  //mnoznik rotacji
  13.  
  14.     private float timeWhenWeStartedFlying;
  15.  
  16.     private bool blockUser;
  17.  
  18.     private Vector2 startSwipe;
  19.     private Vector2 endSwipe;
  20.  
  21.     public float restartTime = 0.3f;
  22.     private bool startedRestartCo; //pomocniczy bool, aby restart dzialal
  23.     // Use this for initialization
  24.     void Start ()
  25.     {
  26.         startPosition = rb.transform.position;
  27.         startRotation = rb.transform.rotation;
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update ()
  32.     {
  33.         if (blockUser)
  34.             return;
  35.         if (Input.GetMouseButtonDown (0))  //zaznaczenie pozycji po nacisnieciu
  36.         {
  37.             startSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
  38.         }
  39.         if (Input.GetMouseButtonUp (0))  //zaznaczenie pozycji po puszczeniu
  40.         {
  41.             endSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
  42.             Swipe();
  43.             blockUser = true;
  44.         }
  45.     }
  46.  
  47.  
  48.     void Swipe()
  49.     {
  50.         rb.isKinematic = false;
  51.  
  52.         timeWhenWeStartedFlying = Time.time;
  53.  
  54.         Vector2 swipe = endSwipe - startSwipe; //stworzenie wektora z dwóch pozycji myszki
  55.  
  56.         int rotationDir;        //ustawienie kierunku rotacji w zaleznosci od wektora
  57.         if (swipe.x >= 0) {     //stworzonego przez gracza
  58.             rotationDir = -1;
  59.         } else {
  60.             rotationDir = 1;   
  61.         }
  62.  
  63.         rb.AddForce (swipe*force, ForceMode.Impulse);  //właściwy wyrzut
  64.  
  65.         rb.AddTorque(0f,0f, torque*rotationDir, ForceMode.Impulse); //właściwa rotacja
  66.     }
  67.  
  68.  
  69.     void OnTriggerEnter(Collider col)
  70.     {
  71.         rb.isKinematic = true;
  72.  
  73.         if (col.tag == "Block") {
  74.         } else {
  75.             Restart ();
  76.         }//zablokowanie obiektu gdy trafi
  77.     }
  78.  
  79.  
  80.     void OnCollisionEnter()
  81.     {
  82.         float timeInAir = Time.time - timeWhenWeStartedFlying;
  83.         if (!rb.isKinematic && timeInAir >= 0.05f)
  84.         {
  85.             Restart();
  86.         }
  87.     }
  88.  
  89.  
  90.     void Restart()
  91.     {
  92.         if(startedRestartCo == false)
  93.         {
  94.             StartCoroutine ("RestartCo");
  95.             startedRestartCo = true;
  96.         }
  97.     }
  98.  
  99.     IEnumerator RestartCo()
  100.     {
  101.         yield return new WaitForSeconds (restartTime);
  102.         rb.velocity = Vector3.zero;
  103.         rb.angularVelocity = Vector3.zero;
  104.         rb.transform.position = startPosition;
  105.         rb.transform.rotation = startRotation;
  106.         rb.isKinematic = false;
  107.         blockUser = false;
  108.         startedRestartCo = false;
  109.     }
  110.  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement