Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Axe : MonoBehaviour {
- public Rigidbody rb;
- private Vector3 startPosition;
- private Quaternion startRotation;
- public float force = 5; //mnoznik sily
- public float torque; //mnoznik rotacji
- private float timeWhenWeStartedFlying;
- private bool blockUser;
- private Vector2 startSwipe;
- private Vector2 endSwipe;
- public float restartTime = 0.3f;
- private bool startedRestartCo; //pomocniczy bool, aby restart dzialal
- // Use this for initialization
- void Start ()
- {
- startPosition = rb.transform.position;
- startRotation = rb.transform.rotation;
- }
- // Update is called once per frame
- void Update ()
- {
- if (blockUser)
- return;
- if (Input.GetMouseButtonDown (0)) //zaznaczenie pozycji po nacisnieciu
- {
- startSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
- }
- if (Input.GetMouseButtonUp (0)) //zaznaczenie pozycji po puszczeniu
- {
- endSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
- Swipe();
- blockUser = true;
- }
- }
- void Swipe()
- {
- rb.isKinematic = false;
- timeWhenWeStartedFlying = Time.time;
- Vector2 swipe = endSwipe - startSwipe; //stworzenie wektora z dwóch pozycji myszki
- int rotationDir; //ustawienie kierunku rotacji w zaleznosci od wektora
- if (swipe.x >= 0) { //stworzonego przez gracza
- rotationDir = -1;
- } else {
- rotationDir = 1;
- }
- rb.AddForce (swipe*force, ForceMode.Impulse); //właściwy wyrzut
- rb.AddTorque(0f,0f, torque*rotationDir, ForceMode.Impulse); //właściwa rotacja
- }
- void OnTriggerEnter(Collider col)
- {
- rb.isKinematic = true;
- if (col.tag == "Block") {
- } else {
- Restart ();
- }//zablokowanie obiektu gdy trafi
- }
- void OnCollisionEnter()
- {
- float timeInAir = Time.time - timeWhenWeStartedFlying;
- if (!rb.isKinematic && timeInAir >= 0.05f)
- {
- Restart();
- }
- }
- void Restart()
- {
- if(startedRestartCo == false)
- {
- StartCoroutine ("RestartCo");
- startedRestartCo = true;
- }
- }
- IEnumerator RestartCo()
- {
- yield return new WaitForSeconds (restartTime);
- rb.velocity = Vector3.zero;
- rb.angularVelocity = Vector3.zero;
- rb.transform.position = startPosition;
- rb.transform.rotation = startRotation;
- rb.isKinematic = false;
- blockUser = false;
- startedRestartCo = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement