SHARE
TWEET

Flick object

a guest Feb 28th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ThrowSimulation : MonoBehaviour
  5. {
  6.     //First Code:
  7.  
  8.     /*
  9.     [Header("Personalize the object Speed")]
  10.     public float maxObjSpd = 40f;
  11.     [Space(10)]
  12.     [Header("Flick Speed")]
  13.     public float flickSpd = 0.4f;
  14.     public float howcls = 9.5f;
  15.     public string respwnnm="";
  16.     float startT, endT, swipeDist, swipeT, tempTime, flickLen;
  17.     Vector2 startPos, endPos;
  18.     float objVel = 0;
  19.     float objSpd = 0;
  20.     Vector3 angle, newPos, vel;
  21.     bool thrown, holding;
  22.     private void Start()
  23.     {
  24.         this.GetComponent<Rigidbody>().useGravity = false;
  25.     }
  26.     void OnTouch() {
  27.         Vector3 touchPos = Input.GetTouch(0).position;
  28.         touchPos.z = Camera.main.nearClipPlane * howcls;
  29.         newPos = Camera.main.ScreenToViewportPoint(touchPos);
  30.         this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, newPos, 80f * Time.deltaTime);
  31.     }
  32.     private void Update()
  33.     {
  34.         if (holding)
  35.         {
  36.             OnTouch();
  37.         }
  38.         else if(thrown)
  39.         {
  40.             return;
  41.         }
  42.         else
  43.         {
  44.         }
  45.  
  46.         if (Input.touchCount > 0)
  47.         {
  48.             Touch _tch = Input.GetTouch(0);
  49.             if (_tch.phase == TouchPhase.Began)
  50.             {
  51.                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
  52.                 RaycastHit hit;
  53.                 if(Physics.Raycast(ray,out hit, 100f))
  54.                 {
  55.                     if (hit.transform == this.transform)
  56.                     {
  57.                         startT = Time.time;
  58.                         startPos = _tch.position;
  59.                         holding = true;
  60.                         transform.SetParent(null);
  61.                     }
  62.                 }else if (_tch.phase == TouchPhase.Ended && holding) {
  63.                     endT = Time.time;
  64.                     endPos = _tch.position;
  65.                     swipeDist = (endPos - startPos).magnitude;
  66.                     swipeT = endT - startT;
  67.  
  68.                     if (swipeT < flickSpd && swipeDist > 100f)
  69.                     {
  70.                         Calspd();
  71.                         MoveAngle();
  72.                         this.GetComponent<Rigidbody>().AddForce(new Vector3((angle.x * objSpd), (angle.y * objSpd), (angle.z * objSpd)));
  73.                         this.GetComponent<Rigidbody>().useGravity = true;
  74.                         holding = false;
  75.                         thrown = true;
  76.                         Invoke("_Reset", 5f);
  77.                     }
  78.                     else {
  79.                         _Reset ();
  80.                     }
  81.                 }
  82.                 if (startT > 0)
  83.                 {
  84.                     tempTime = Time.time - startT;
  85.                 }
  86.                 if (tempTime > flickSpd)
  87.                 {
  88.                     startT = Time.time;
  89.                     startPos = _tch.position;
  90.                 }
  91.             }
  92.         }
  93.         void _Reset()
  94.         {
  95.             Transform respwnP = GameObject.Find(respwnnm).transform;
  96.             this.gameObject.transform.position = respwnP.position;
  97.             this.gameObject.transform.rotation = respwnP.rotation;
  98.             this.GetComponent<Rigidbody>().velocity = Vector3.zero;
  99.             this.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
  100.             this.GetComponent<Rigidbody>().useGravity= false;
  101.             thrown = holding = false;
  102.         }
  103.         void Calspd()
  104.         {
  105.             flickLen = swipeDist;
  106.             if (swipeT > 0)
  107.             {
  108.                 objVel = flickLen / (flickLen - swipeT);
  109.             }
  110.             objSpd = objVel * 50;
  111.             objSpd = objSpd - (objSpd * 1.7f);
  112.             if(objSpd<= -maxObjSpd)
  113.             {
  114.                 objSpd = -maxObjSpd;
  115.             }
  116.             swipeT = 0;
  117.         }
  118.         void MoveAngle()
  119.         {
  120.             angle = Camera.main.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(endPos.y + 50f, (Camera.main.GetComponent<Camera>().nearClipPlane - howcls)));
  121.         }
  122.     }
  123. */
  124.  
  125. //Second Code:
  126.  
  127.  /*  
  128.     public Transform Target;
  129.     public float firingAngle = 45.0f;
  130.     public float gravity = 9.8f;
  131.  
  132.     public Transform Projectile;      
  133.     private Transform myTransform;
  134.    
  135.     void Awake()
  136.     {
  137.         myTransform = transform;      
  138.     }
  139.  
  140.     public void Start()
  141.     {          
  142.         //StartCoroutine(SimulateProjectile());
  143.     }
  144.  
  145.  
  146.     public IEnumerator SimulateProjectile()
  147.     {
  148.         Debug.Log("Simulate Projectile has activated");
  149.         // Short delay added before Projectile is thrown
  150.        // yield return new WaitForSeconds(1.5f);
  151.        
  152.         // Move projectile to the position of throwing object + add some offset if needed.
  153.         //Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);
  154.        
  155.         // Calculate distance to target
  156.         float target_Distance = Vector3.Distance(Projectile.position, Target.position);
  157.  
  158.         // Calculate the velocity needed to throw the object to the target at specified angle.
  159.         float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
  160.  
  161.         // Extract the X  Y componenent of the velocity
  162.         float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
  163.         float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
  164.  
  165.         // Calculate flight time.
  166.         float flightDuration = target_Distance / Vx;
  167.    
  168.         // Rotate projectile to face the target.
  169.         Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);
  170.        
  171.         float elapse_time = 0;
  172.  
  173.         while (elapse_time < (flightDuration))
  174.         {
  175.             Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
  176.            
  177.             elapse_time += Time.deltaTime;
  178.  
  179.             yield return null;
  180.         }
  181.     } */
  182.  
  183.     //Third Code:
  184.  
  185.     Vector2 startPos, endPos, dir;
  186.     float touchStartT, touchEndT, timeInt;
  187.  
  188.     [SerializeField]
  189.     float touchForceXY = 2f;
  190.     [SerializeField]
  191.     float touchForceZ = 50f;
  192.  
  193.     Rigidbody rb;
  194.  
  195.     private void Start()
  196.     {
  197.         rb = GetComponent<Rigidbody>();
  198.     }
  199.     private void Update()
  200.     {
  201.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  202.         {
  203.             touchStartT = Time.time;
  204.             startPos = Input.GetTouch(0).position;
  205.         }
  206.         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
  207.         {
  208.             touchEndT = Time.time;
  209.  
  210.             timeInt = touchEndT - touchStartT;
  211.             endPos = Input.GetTouch(0).position;
  212.             dir = startPos - endPos;
  213.             rb.isKinematic = false;
  214.             rb.AddForce(-dir.x * touchForceXY, -dir.y * touchForceXY, touchForceZ / timeInt);
  215.         }
  216.     }
  217.  
  218. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top