Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class ThrowSimulation : MonoBehaviour
- {
- //First Code:
- /*
- [Header("Personalize the object Speed")]
- public float maxObjSpd = 40f;
- [Space(10)]
- [Header("Flick Speed")]
- public float flickSpd = 0.4f;
- public float howcls = 9.5f;
- public string respwnnm="";
- float startT, endT, swipeDist, swipeT, tempTime, flickLen;
- Vector2 startPos, endPos;
- float objVel = 0;
- float objSpd = 0;
- Vector3 angle, newPos, vel;
- bool thrown, holding;
- private void Start()
- {
- this.GetComponent<Rigidbody>().useGravity = false;
- }
- void OnTouch() {
- Vector3 touchPos = Input.GetTouch(0).position;
- touchPos.z = Camera.main.nearClipPlane * howcls;
- newPos = Camera.main.ScreenToViewportPoint(touchPos);
- this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, newPos, 80f * Time.deltaTime);
- }
- private void Update()
- {
- if (holding)
- {
- OnTouch();
- }
- else if(thrown)
- {
- return;
- }
- else
- {
- }
- if (Input.touchCount > 0)
- {
- Touch _tch = Input.GetTouch(0);
- if (_tch.phase == TouchPhase.Began)
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
- RaycastHit hit;
- if(Physics.Raycast(ray,out hit, 100f))
- {
- if (hit.transform == this.transform)
- {
- startT = Time.time;
- startPos = _tch.position;
- holding = true;
- transform.SetParent(null);
- }
- }else if (_tch.phase == TouchPhase.Ended && holding) {
- endT = Time.time;
- endPos = _tch.position;
- swipeDist = (endPos - startPos).magnitude;
- swipeT = endT - startT;
- if (swipeT < flickSpd && swipeDist > 100f)
- {
- Calspd();
- MoveAngle();
- this.GetComponent<Rigidbody>().AddForce(new Vector3((angle.x * objSpd), (angle.y * objSpd), (angle.z * objSpd)));
- this.GetComponent<Rigidbody>().useGravity = true;
- holding = false;
- thrown = true;
- Invoke("_Reset", 5f);
- }
- else {
- _Reset ();
- }
- }
- if (startT > 0)
- {
- tempTime = Time.time - startT;
- }
- if (tempTime > flickSpd)
- {
- startT = Time.time;
- startPos = _tch.position;
- }
- }
- }
- void _Reset()
- {
- Transform respwnP = GameObject.Find(respwnnm).transform;
- this.gameObject.transform.position = respwnP.position;
- this.gameObject.transform.rotation = respwnP.rotation;
- this.GetComponent<Rigidbody>().velocity = Vector3.zero;
- this.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
- this.GetComponent<Rigidbody>().useGravity= false;
- thrown = holding = false;
- }
- void Calspd()
- {
- flickLen = swipeDist;
- if (swipeT > 0)
- {
- objVel = flickLen / (flickLen - swipeT);
- }
- objSpd = objVel * 50;
- objSpd = objSpd - (objSpd * 1.7f);
- if(objSpd<= -maxObjSpd)
- {
- objSpd = -maxObjSpd;
- }
- swipeT = 0;
- }
- void MoveAngle()
- {
- angle = Camera.main.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(endPos.y + 50f, (Camera.main.GetComponent<Camera>().nearClipPlane - howcls)));
- }
- }
- */
- //Second Code:
- /*
- public Transform Target;
- public float firingAngle = 45.0f;
- public float gravity = 9.8f;
- public Transform Projectile;
- private Transform myTransform;
- void Awake()
- {
- myTransform = transform;
- }
- public void Start()
- {
- //StartCoroutine(SimulateProjectile());
- }
- public IEnumerator SimulateProjectile()
- {
- Debug.Log("Simulate Projectile has activated");
- // Short delay added before Projectile is thrown
- // yield return new WaitForSeconds(1.5f);
- // Move projectile to the position of throwing object + add some offset if needed.
- //Projectile.position = myTransform.position + new Vector3(0, 0.0f, 0);
- // Calculate distance to target
- float target_Distance = Vector3.Distance(Projectile.position, Target.position);
- // Calculate the velocity needed to throw the object to the target at specified angle.
- float projectile_Velocity = target_Distance / (Mathf.Sin(2 * firingAngle * Mathf.Deg2Rad) / gravity);
- // Extract the X Y componenent of the velocity
- float Vx = Mathf.Sqrt(projectile_Velocity) * Mathf.Cos(firingAngle * Mathf.Deg2Rad);
- float Vy = Mathf.Sqrt(projectile_Velocity) * Mathf.Sin(firingAngle * Mathf.Deg2Rad);
- // Calculate flight time.
- float flightDuration = target_Distance / Vx;
- // Rotate projectile to face the target.
- Projectile.rotation = Quaternion.LookRotation(Target.position - Projectile.position);
- float elapse_time = 0;
- while (elapse_time < (flightDuration))
- {
- Projectile.Translate(0, (Vy - (gravity * elapse_time)) * Time.deltaTime, Vx * Time.deltaTime);
- elapse_time += Time.deltaTime;
- yield return null;
- }
- } */
- //Third Code:
- Vector2 startPos, endPos, dir;
- float touchStartT, touchEndT, timeInt;
- [SerializeField]
- float touchForceXY = 2f;
- [SerializeField]
- float touchForceZ = 50f;
- Rigidbody rb;
- private void Start()
- {
- rb = GetComponent<Rigidbody>();
- }
- private void Update()
- {
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
- {
- touchStartT = Time.time;
- startPos = Input.GetTouch(0).position;
- }
- if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
- {
- touchEndT = Time.time;
- timeInt = touchEndT - touchStartT;
- endPos = Input.GetTouch(0).position;
- dir = startPos - endPos;
- rb.isKinematic = false;
- rb.AddForce(-dir.x * touchForceXY, -dir.y * touchForceXY, touchForceZ / timeInt);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement